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.
Example 1:
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)".
Example 2:
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. 对于树,第一步一定要想到用递归。
public class Solution {
public String tree2str(TreeNode root) {
if (root == null) return "";
String rootValue = String.valueOf(root.val);
String leftValue = tree2str(root.left);
String rightValue = tree2str(root.right); if (leftValue.equals("") && rightValue.equals("")) {
return rootValue;
} else if (leftValue.equals("")) {
return String.format("%s()(%s)", rootValue, rightValue);
} else if (rightValue.equals("")) {
return String.format("%s(%s)", rootValue, leftValue);
} else {
return String.format("%s(%s)(%s)", rootValue, leftValue, rightValue);
}
}
}
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 ...
- [Algorithm] Construct String from Binary Tree
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
随机推荐
- 2g 大文件上传
核心原理: 该项目核心就是文件分块上传.前后端要高度配合,需要双方约定好一些数据,才能完成大文件分块,我们在项目中要重点解决的以下问题. * 如何分片: * 如何合成一个文件: * 中断了从哪个分片开 ...
- 使用matplotlib绘制常用图表(2)-常用图标设置
一.使用subplots绘制子图 import numpy as np from matplotlib import pyplot as plt %matplotlib inline x = np.a ...
- Cow and Snacks
D. Cow and Snacks 参考:Codeforces 1209D. Cow and Snacks 思路:利用并查集,构建一个生成树,然后树的边数就是能够开心的客人的人数.用一个条件fin ...
- Keras学习笔记三:一个图像去噪训练并离线测试的例子,基于mnist
训练模型需要的数据文件有: MNIST_data文件夹下的mnist_train.mnist_test.noisy_train.noisy_test.train文件夹下60000个图片,test下10 ...
- Rust格式化输出
打印操作由 https://doc.rust-lang.org/std/fmt/ 里面所定义的一系列宏来处理,包括: format!:将格式化文本写到字符串(String).(译注:字符串是返 回值不 ...
- uniapp导航栏自定义按钮及点击事件
本文链接:https://blog.csdn.net/qq_33807889/article/details/89945674第一步:显示按钮假设页面名称为:AddSort 在pages.json中找 ...
- LeetCode 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)
题目描述 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 解题思路 由 ...
- VI快捷键速记
enjoy :P
- pycloudtag_wordcloud 中英文标签云
https://study.163.com/course/courseMain.htm?courseId=1006383008&share=2&shareId=400000000398 ...
- win + T 快捷键
和 alt + tab 不同,win + T会在 在任务栏间的程序间进行switch