606. Construct String from Binary Tree 【easy】
606. Construct String from Binary Tree 【easy】
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.
差一点AC的代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
string tree2str(TreeNode* t) {
string result; if (t == NULL) {
return "";
} result += to_string(t->val); if (t->left) {
result += "(" + tree2str(t->left) + ")";
} if (t->right) {
result += "(" + tree2str(t->right) + ")";
} return result;
}
};

实际上对应的图如下图,可以发现需要对左子树为空,右子树不为空的情况做个特殊判断。

解法一:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
string tree2str(TreeNode* t) {
string result; if (t == NULL) {
return "";
} result += to_string(t->val); if (t->left) {
result += "(" + tree2str(t->left) + ")";
}
else if (t->right) {
result += "()";
} if (t->right) {
result += "(" + tree2str(t->right) + ")";
} return result;
}
};
上面代码中第24 ~ 26行就是对左子树为空,右子树不为空的情况做的特殊判断。
解法二:
class Solution {
public:
string tree2str(TreeNode* t) {
return !t ? "" : to_string(t->val) + (t->left ? "(" + tree2str(t->left) + ")" : t->right ? "()" : "")
+ (t->right ? "(" + tree2str(t->right) + ")" : "");
}
};
参考@alexander 的代码。
解法三:
public class Solution {
public String tree2str(TreeNode t) {
if (t == null) return "";
String result = t.val + "";
String left = tree2str(t.left);
String right = tree2str(t.right);
if (left == "" && right == "") return result;
if (left == "") return result + "()" + "(" + right + ")";
if (right == "") return result + "(" + left + ")";
return result + "(" + left + ")" + "(" + right + ")";
}
}
最后集中起来再判断的思路很好,参考@shawngao 的代码。
解法四:
public String tree2str(TreeNode t) {
StringBuilder sb = new StringBuilder();
helper(sb, t);
return sb.toString();
}
public void helper(StringBuilder sb, TreeNode t) {
if (t != null) {
sb.append(t.val);
if(t.left != null || t.right != null) {
sb.append("(");
helper(sb, t.left);
sb.append(")");
if(t.right != null) {
sb.append("(");
helper(sb, t.right);
sb.append(")");
}
}
}
}
606. Construct String from Binary Tree 【easy】的更多相关文章
- 【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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 日期 题目地址:https://l ...
- 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 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 ...
- 606. Construct String from Binary Tree 从二叉树中构建字符串
[抄题]: You need to construct a string consists of parenthesis and integers from a binary tree with th ...
- Python 解LeetCode:606 Construct String from Binary Tree
题目描述:用先序遍历的方式把二叉树转换成字符串,其中结点用括号分割,具体示例见题目链接 思路: 先序遍历,先把根结点的值转化成字符串,然后递归的把左右子树的值转化成字符串 把1中的根结点和左右子结点的 ...
随机推荐
- 【数位dp】hdu2089 不要62
http://www.cnblogs.com/xiaohongmao/p/3473599.html #include<cstdio> using namespace std; int n, ...
- 【二分答案】【分块答案】【字符串哈希】【set】bzoj2946 [Poi2000]公共串
我们二分/分块枚举答案x,暴力把除了最短的字符串以外的其他字符串的x长度子串哈希搞出来,分别扔到set里. 然后暴力枚举最短的字符串的x长度字串,查看是否在全部的set里出现过. #include&l ...
- FireDac Pooling
1.建立FDManager的ConnectionDef.并设置此Pooling为True. 2.建立Thread类进行多个FDConnection连接DB. 3.本列是oracle远程数据.如下图: ...
- java读写文件及保留指定位小数
1)先上代码: public static void main(String[] args)throws IOException{ double[][] B=new double[1043][2102 ...
- iOS 修改APP工程名字
我们在iOS开发中,难免会遇到项目做到一半要改名字的情况.如果项目名差的太大,工程名看起来总是不舒服的,就会想着为工程改个贴切的名字,那么你就为用到本文记录的内容. 如何修改工程名呢? 下面我就拿一个 ...
- MathType如何插入竖直线
不用键盘上的竖线,用左竖直线和右竖直线.
- TCP Socket一些东西
1. 若connect失败该套接字不可再用,必须close当前套接字,重新调用socket. 手册上注明连接失败后, socket的状态是未知的, 所以再次connect, 可能成功, 也可能失败. ...
- redis深入研究
Redis 设计思路学习与总结 https://cloud.tencent.com/developer/article/1004464 Redis 设计思路学习与总结 下半年利用空余时间研究和分析了部 ...
- Linux的PCI驱动分析
1. 关键数据结构 PCI设备上有三种地址空间:PCI的I/O空间.PCI的存储空间和PCI的配置空间.CPU可以访问PCI设备上的所有地址空间,其中I/O空间和存储空间提供给设备驱动程序使用,而配置 ...
- web.xml文件中配置mime下载文件类型(转)
转自:http://5aijava.iteye.com/blog/166600 TOMCAT在默认情况下下载.rar的文件是把文件当作text打开,以至于IE打开RAR文件为乱码,如果遇到这种情况时不 ...