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】的更多相关文章

  1. 【Leetcode_easy】606. Construct String from Binary Tree

    problem 606. Construct String from Binary Tree 参考 1. Leetcode_easy_606. Construct String from Binary ...

  2. 【LeetCode】606. Construct String from Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:先序遍历 日期 题目地址:https://l ...

  3. 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 ...

  4. 606. Construct String from Binary Tree

    You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...

  5. LeetCode 606 Construct String from Binary Tree 解题报告

    题目要求 You need to construct a string consists of parenthesis and integers from a binary tree with the ...

  6. [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 ...

  7. 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 ...

  8. 606. Construct String from Binary Tree 从二叉树中构建字符串

    [抄题]: You need to construct a string consists of parenthesis and integers from a binary tree with th ...

  9. Python 解LeetCode:606 Construct String from Binary Tree

    题目描述:用先序遍历的方式把二叉树转换成字符串,其中结点用括号分割,具体示例见题目链接 思路: 先序遍历,先把根结点的值转化成字符串,然后递归的把左右子树的值转化成字符串 把1中的根结点和左右子结点的 ...

随机推荐

  1. small test on 5.29 night T1

    可以发现题目的重点是在第一个部分,因为只要信心值我们求出来了,那么第二问就是一个简单的最长上升子序列问题了,所以接下来只讲第一问. #include<iostream> #include& ...

  2. [Contest20180405]抑制「超我」

    古明地恋(koishi)和计算器(calculator)是好朋友.恋恋有一个神奇的计算器,可以进行两个数在模$2^n$意义下的加法运算.计算器上有一个寄存器,一开始寄存器中的数为$0$,每当恋恋输入一 ...

  3. Awesome图标 | 如何在某些编辑软件中使用Font Awesome字体图标

    文章目录 Font Awesome 字体图标 在某些编辑软件中使用 Font Awesome 字体图标 Font Awesome 为您提供可缩放矢量图标,它可以被定制大小.颜色.阴影以及任何可以用 C ...

  4. ios中将事件同步到系统日历

    //获取日历事件 EKEventStore* eventStore = [[EKEventStorealloc] init]; NSDate* ssdate = [NSDatedateWithTime ...

  5. C# 6.0语法新特性体验(二)

    之前我在文章通过Roslyn体验C# 6.0的新语法中介绍了一些C# 6.0的语法特性,现在随着Visual Studio 14 CTP3的发布,又陆续可以体验一些新的特性了,这里简单的介绍一下之前没 ...

  6. Mysql -- Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’解决方法

    启动mysql 报错: ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/m ...

  7. linux/mac系统的软链接文件与硬链接文件

    1.硬连接只能使用在文件上,不可以使用在文件夹上.至于文件前面的硬链接数字的含义如下: 如图标注区,为硬连接的数量,文件前的数字1表示没有硬链接.文件夹前面的数字至少是2,含义是这个文件夹是空文件夹, ...

  8. NYOJ 1058 部分和问题 【DFS】

    部分和问题 时间限制:1000 ms  |  内存限制:65535 KB 难度:2 描写叙述 给定整数a1.a2........an,推断能否够从中选出若干数.使它们的和恰好为K. 输入 首先,n和k ...

  9. 深入NIO Socket实现机制(转)

    http://www.jianshu.com/p/0d497fe5484a# 前言 Java NIO 由以下几个核心部分组成: Buffer Channel Selector 以前基于net包进行so ...

  10. JAVA实现https单向认证

    //关于http 须要两个jar包 httpclient-4.0.jar httpcore-4.0.1.jar private static final HttpClient httpClient = ...