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)'

最新文章

  1. OEL上使用yum install oracle-validated 简化主机配置工作
  2. Android热修复之微信Tinker使用初探
  3. ALV中处理过滤掉的行
  4. HDU 3854 Glorious Array(树状数组)
  5. 学习资料 数据查询语言DQL
  6. Swift的一些基础内容
  7. RTDX target application does not match emulation protocol!
  8. [转]C++ list 类学习笔记
  9. [转载]浅析Windows安全相关的一些概念
  10. JavaSE学习总结第03天_Java基础语法2
  11. 改动file header (測)
  12. Intent传值的学习
  13. 基于Python+Django重定向的例子
  14. unittest单元测试框架中的参数化及每个用例的注释
  15. 基于zookeeper的高可用Hadoop HA集群安装
  16. 2017-11-26 编程语言试验之Antlr4+Java实现"圈2"
  17. apicloud开发笔记
  18. QT+VS中ui不能声明为指针?
  19. linux中创建一个回收站
  20. 手把手教你用 Keras 实现 LSTM 预测英语单词发音

热门文章

  1. 【Go命令教程】4. go get
  2. ERROR 2002 (HY000): Can't connect to local MySQL server through socket
  3. EEPLAT学习
  4. 委托、Lambda表达式、事件系列04,委托链是怎样形成的, 多播委托, 调用委托链方法,委托链异常处理
  5. Net Framework 2.0 MSI returned error code 1603解决方法
  6. ArcGIS Desktop水文计算
  7. C#编程(五十二)----------有序列表
  8. Java/JSP获得客户端网卡MAC地址的三种方法解析
  9. fragment做成选项卡,tab效果。 fragment+RadioGroup
  10. 使用 ssh -R 建立反向/远程TCP端口转发代理