[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 ...
随机推荐
- [置顶] js 实现 <input type="file" /> 文件上传
在开发中,文件上传必不可少,<input type="file" /> 是常用的上传标签,但是它长得又丑.浏览的字样不能换,我们一般会用让,<input type ...
- How do I debug a published XBAP file in VS2010?
I need to debug a full-trust application either by specifying a URL or, ideally, from within the web ...
- C# 其他图片格式转emf
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- WordPress主题开发:更换后台编辑器
这里我更换为KindEditor 1.下载插件 https://wordpress.org/plugins/kindeditor-for-wordpress/ 2.解压至wordpress目录下的/w ...
- .NET:Attribute 入门(内训教程)
背景 接触过的语言中,C#(.NET 平台的多数语言都支持).Java 和 Python 都支持这个特性,本文重点介绍 C# 中的应用,这里简单的对 C#.java 和 Python 中的 Attri ...
- SharePoint 取消分享时的默认发邮件
前言 最近遇到一个需求,就是sharepoint默认分享的时候,会默认勾选发送邮件的功能.而用户,经常会用到分享的功能,但是不需要发送邮件,需要默认不勾选这个操作. 这样,就需要修改sharepoin ...
- android之卸载反馈的功能
感谢这位大神:http://www.eoeandroid.com/thread-317728-1-1.html zip包里面有讲解的试用方法,和如何试用ndk编译的方法,本人亲身试验,确实可用,现做一 ...
- ExtJS 4.2 教程-03:使用Ext.define自定义类
转载自起飞网,原文地址:http://www.qeefee.com/extjs-course-3-define-classes ExtJS 4.2 教程-01:Hello ExtJS ExtJS 4. ...
- 利用ViewStub来延迟加载视图
很多情况下,我们的视图可能会随着用户的操作的不同而变化,比如一个新的页面包含多个控件,但仅仅在用户点击这个按钮后,所有的控件才能完全显示.也就是说一上来可能就显示一个控件,点击按钮后把其他隐藏的控件再 ...
- 以绑定的方式来启动service
先说下原理,之前我们的启动service就是用startService来启动的,这是显式启动.启动后我们无法得到service中的数据,也无法知道它执行的状态,如果我们要启动它的activity和它建 ...