[Algorithm] Construct String from Binary Tree
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的更多相关文章
- 606. Construct String from Binary Tree 【easy】
606. Construct String from Binary Tree [easy] You need to construct a string consists of parenthesis ...
- 【Leetcode_easy】606. Construct String from Binary Tree
problem 606. Construct String from Binary Tree 参考 1. Leetcode_easy_606. Construct String from Binary ...
- 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 ...
- 606. Construct String from Binary Tree
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
- [LeetCode] Construct String from Binary Tree 根据二叉树创建字符串
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
- [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 ...
- LeetCode 606 Construct String from Binary Tree 解题报告
题目要求 You need to construct a string consists of parenthesis and integers from a binary tree with the ...
- [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 ...
- 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 ...
随机推荐
- Visual Studio新的 .csporj 文件
Visual Studio新的 .csporj 文件非常方便,虽然目前还不支持WPF.WinForm等工程,但应用到控制台程序,类库还是没有任何问题的.只需要简单的用如下内容替换老的csproj即可: ...
- kettle的基本介绍
Kettle 主要内容: 一.ETL介绍 二.Kettle介绍 三.Java调用Kettle API 一.ETL介绍 1. ETL是什么? 1).ETL分别是“Extract”.“ Transform ...
- python dtrace 安装与应用
https://ipfans.github.io/2016/09/tracing-python-program-with-dtrace/?utm_source=tuicool&utm_medi ...
- 关于UIImageView的显示问题——居中显示或者截取图片的中间部分显示
我们都知道在ios中,每一个UIImageView都有他的frame大小,但是如果图片的大小和这个frame的大小不符合的时候会怎么样呢?在默认情况,图片会被压缩或者拉伸以填满整个区域. 通过查看UI ...
- C#编程(六十二)---------LINQ标准的查询操作符
LINQ标准的查询操作符 首先我们来看一下LINQ的操作符,可根据查询操作符的操作”类型”进行分类,如把它们分成投影,限制,排序,联接,分组,串联,聚合,集合,生成,转换,元素,相等,量词,分割等. ...
- Fidder发送Get、POST请求
Composer: 1.Get请求 a) 请求头加上: Content-Type: application/json; charset=utf-8 b) url:http://localh ...
- Kettle优化就这么多
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/ClamReason/article/details/49930479 Kettle正常转换速度 场景 ...
- Spring Websocket实现文本、图片、声音、文件下载及推送、接收及显示(集群模式)
相关环境 Nginx,Spring5.x当前(要选择4.0+),tomcat8.x,Quartz 2.x集群(实际运用是Quartz的集群模式和单机模式共存的) 测试面页:http://sms.rey ...
- ExtJS 4.2 教程-01:Hello ExtJS
转载自起飞网,原文地址:http://www.qeefee.com/extjs-course-1-hello-extjs, 本文还发布在了ExtJS教程网站起飞网上面,如果转载请保留本段声明,谢谢合作 ...
- 监听Listview的滚动状态,是否滚动到了顶部或底部
/** * @author:Jack Tony * @description : 监听listview的滑动状态,如果到了顶部就刷新数据 * @date :2015年2月9日 */ private c ...