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. C:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\文件不断增长,如何处理?

    很久没有写博了.最近半年除了忙活布置新家和过年期间走亲访友之外,都是在公司处理一些项目中的杂事:连家里买的很多书都停下来没看了,感觉这段时间在事业和学习上一直都是忙忙碌碌,却又碌碌无为. 吐槽完,说正 ...

  2. Python可视化库

    转自小小蒲公英原文用Python可视化库 现如今大数据已人尽皆知,但在这个信息大爆炸的时代里,空有海量数据是无实际使用价值,更不要说帮助管理者进行业务决策.那么数据有什么价值呢?用什么样的手段才能把数 ...

  3. Java 打包下载服务器上选中的文件或目录(带进度条提示)

    http://www.cnblogs.com/interdrp/p/6702482.html 由于此次文件管理系统的升级确实给我们带来了很多方便且在性能上有很大提升,经过这段时间的使用 也发现了些问题 ...

  4. java List集合记录 ArrayList和LinkedList的区别

    一般大家都知道ArrayList和LinkedList的大致区别:      1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构.      2.对于随机访问 ...

  5. 在websocket中怎么样注入service类

    最近项目中用到了websocket,遇到很多问题,其中一个是@ServerEndpoint修饰的类无法注入其他的bean,注入的对象都是null,在网上找了好多资料,无意中发现一个朋友的答案给了思路. ...

  6. spring boot对输入的字符串进行html转码

    可以使用HtmlUtils这个类进行操作.具体的可以参考API,或者点出来看.

  7. Android之防止反编译技巧

    1. 判断程序是否运行在模拟器上 boolean isRunningInEmualtor() { boolean qemuKernel = false; Process process = null; ...

  8. Android之关于MAC把java7改为java6的方法

    先来个草草草,某天手贱有java6升级为java7了,然后用ant打包发布,然后再一次草草草,居然有冲突勒,网上找了一堆...无果,最后想起直接在.bash_profile上配置环境试试吧,居然通了, ...

  9. 将Linux默认的OpenJDK替换为Oracle JDK

    在使用Logstash安装插件的时候,发生了一个错误,如下: ERROR: Something went wrong when installing logstash-input-jdbc, mess ...

  10. python读取yaml配置文件

    支持多种语言:python.js.golang.java.c.c++ YAML 语言(发音 /ˈjæməl/ )的设计目标,就是方便人类读写.它实质上是一种通用的数据串行化格式. 它的基本语法规则如下 ...