You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way.

The null node needs to be represented by empty parenthesis pair "()". And you need to omit all the empty parenthesis pairs that don't affect the one-to-one mapping relationship between the string and the original binary tree.

Input: Binary tree: [1,2,3,4]
1
/ \
2 3
/
4 Output: "1(2(4))(3)"

Explanation: Originallay it needs to be "1(2(4)())(3()())",
but you need to omit all the unnecessary empty parenthesis pairs.
And it will be "1(2(4))(3)".
Input: Binary tree: [1,2,3,null,4]
1
/ \
2 3
\
4 Output: "1(2()(4))(3)"

Explanation: Almost the same as the first example,
except we can't omit the first parenthesis pair to break the one-to-one mapping relationship between the input and the output.
function Node(val) {
return {
val,
left: null,
right: null
};
} function Tree() {
return {
root: null,
addLeft(val, root) {
const node = Node(val);
root.left = node;
return root.left;
},
addRight(val, root) {
const node = Node(val);
root.right = node;
return root.right;
}
};
} function printTree(root) {
let result = "";
function print(root, result = '') {
if (root === null) {
return "";
} // the leaf node
if (root.left === null && root.right === null) {
return `${root.val}`;
} //if has left but no right
if (root.left !== null && root.right === null) {
return `${root.val}(${root.left.val})`;
} // if has no left but has right
if (root.left === null && root.right !== null) {
return `${root.val}()(${root.right.val})`;
} result += root.val
result += `(${print(root.left, result)})`;
result += `(${print(root.right, result)})`;
return result
} result += print(root, result); return result;
} const t1 = new Tree();
t1.root = Node(1);
const n1 = t1.addLeft(2, t1.root);
t1.addRight(3, t1.root);
t1.addLeft(4, n1);
console.log(printTree(t1.root)); // '1(2(4))(3)' const t2 = new Tree();
t2.root = Node(1);
const n2 = t1.addLeft(2, t2.root);
t2.addRight(3, t2.root);
t2.addRight(4, n2);
console.log(printTree(t2.root)); //'1(2()(4))(3)'

[Algorithm] Construct String from Binary Tree的更多相关文章

  1. 606. Construct String from Binary Tree 【easy】

    606. Construct String from Binary Tree [easy] You need to construct a string consists of parenthesis ...

  2. 【Leetcode_easy】606. Construct String from Binary Tree

    problem 606. Construct String from Binary Tree 参考 1. Leetcode_easy_606. Construct String from Binary ...

  3. LeetCode 606. Construct String from Binary Tree (建立一个二叉树的string)

    You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...

  4. 606. Construct String from Binary Tree

    You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...

  5. [LeetCode] Construct String from Binary Tree 根据二叉树创建字符串

    You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...

  6. [Swift]LeetCode606. 根据二叉树创建字符串 | Construct String from Binary Tree

    You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...

  7. LeetCode 606 Construct String from Binary Tree 解题报告

    题目要求 You need to construct a string consists of parenthesis and integers from a binary tree with the ...

  8. [LeetCode&Python] Problem 606. Construct String from Binary Tree

    You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...

  9. LeetCode 606. Construct String from Binary Tree根据二叉树创建字符串 (C++)

    题目: You need to construct a string consists of parenthesis and integers from a binary tree with the ...

随机推荐

  1. 用sourceTree提交代码时遇到的问题

    xcuserstate 每次并没有改什么东西,只是随便点了几下就会出现的未暂存文件,可以对其停止追踪! 右键,停止追踪,提交,推送.以后就不会再有这个讨厌的文件出现了! 还没有提交就拉代码的囧境 有的 ...

  2. ajax jquery 异步表单验证

    文件目录: html代码: <html> <head> <title>异步表单验证</title> <script type='text/java ...

  3. win10如何彻底删除Gis|彻底卸载ArcGis的方法说明

    ArcGIS产品线为用户提供一个可伸缩的,全面的GIS平台.ArcObjects包含了许多的可编程组件,从细粒度的对象(例如单个的几何对象)到粗粒度的对象(例如与现有ArcMap文档交互的地图对象)涉 ...

  4. ibatis.net:第六天,QueryForList

    xml <statement id="FindOrdersByCustomer" parameterClass="string" resultClass= ...

  5. 记linux下rm误删bin文件的解决方式

    平常有个坏习惯,删文件为了快点,喜欢用rm xx*,删除一些关键词文件.今天为了删/bin下几个含有mix关键词的文件,使用命令rm mix*.手贱,mix和*之间多了个空格...灾难发生了!bin下 ...

  6. [Web 前端] ECMAScript5之StrictMode

    cp from : https://www.cnblogs.com/giggle/p/5252185.html ECMAScript5引入一个严格模式的概念(Strict Mode). 它的作用就是不 ...

  7. maven单测生成覆盖率报告---Jacoco的使用

    JaCoCo介绍 一.JaCoCo简述 JaCoCo是一个开源的覆盖率工具,它针对的开发语言是java,其使用方法很灵活,可以嵌入到Ant.Maven中:可以作为Eclipse插件,可以使用其Java ...

  8. (使用STL中的数据结构进行编程7.3.15)UVA 630 Anagrams (II)(求一个单词在字典中出现的次数)

    /* * UVA_630.cpp * * Created on: 2013年11月4日 * Author: Administrator */ #include <iostream> #in ...

  9. cesium js学习一加载三维模型【转】

    http://blog.csdn.net/tangyajun_168/article/details/50936698 最近项目中用到室外三维模型与室内三维地图交互,室外三维模型的加载我们采用了ces ...

  10. vRealize Automation的REST API Reference在哪里可以看到?

    两个地方: 1. VMware官网可以查看. http://pubs.vmware.com/vrealize-automation-71/topic/com.vmware.vra.restapi.do ...