leetcode 606. Construct String from Binary Tree
来源:互联网 发布:淘宝店铺名怎么修改 编辑:程序博客网 时间:2024/06/05 04:24
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.
package leetcode;public class Construct_String_from_Binary_Tree_606 {public String tree2str(TreeNode t) {if(t==null){return "";}String string=t.val+"";if(t.left==null&&t.right==null){return string;}if(t.left!=null){string=string+"("+tree2str(t.left)+")";if(t.right!=null){string=string+"("+tree2str(t.right)+")";}}else{string=string+"()";if(t.right!=null){string=string+"("+tree2str(t.right)+")";}}return string;}public static void main(String[] args) {// TODO Auto-generated method stubConstruct_String_from_Binary_Tree_606 c=new Construct_String_from_Binary_Tree_606();TreeNode root=new TreeNode(1);root.left=new TreeNode(2);root.right=new TreeNode(3);root.left.right=new TreeNode(4);System.out.println(c.tree2str(root));}}大神用了递归和迭代。递归思路跟我一样:
public class Solution { public String tree2str(TreeNode t) { if(t==null) return ""; if(t.left==null && t.right==null) return t.val+""; if(t.right==null) return t.val+"("+tree2str(t.left)+")"; return t.val+"("+tree2str(t.left)+")("+tree2str(t.right)+")"; }}迭代是用栈。https://leetcode.com/problems/construct-string-from-binary-tree/#/solution有图解。现在leetcode搞的这个真是太棒了,通俗易懂。
public class Solution { public String tree2str(TreeNode t) { if (t == null) return ""; Stack < TreeNode > stack = new Stack < > (); stack.push(t); Set < TreeNode > visited = new HashSet < > (); StringBuilder s = new StringBuilder(); while (!stack.isEmpty()) { t = stack.peek(); if (visited.contains(t)) { stack.pop(); s.append(")"); } else { visited.add(t); s.append("(" + t.val); if (t.left == null && t.right != null) s.append("()"); if (t.right != null) stack.push(t.right); if (t.left != null) stack.push(t.left); } } return s.substring(1, s.length() - 1); }}
阅读全文
0 0
- [leetcode]606. Construct String from Binary Tree
- [leetcode]: 606. Construct String from Binary Tree
- [LeetCode] 606. Construct String from Binary Tree
- Leetcode 606. Construct String from Binary Tree
- LeetCode: 606. Construct String from Binary Tree
- [LeetCode]606. Construct String from Binary Tree
- leetcode 606. Construct String from Binary Tree
- LeetCode 606. Construct String from Binary Tree
- leetcode: 606. Construct String from Binary Tree
- Leetcode-606. Construct String from Binary Tree
- leetcode-606. Construct String from Binary Tree
- leetcode-606. Construct String from Binary Tree
- Leetcode-536. Construct Binary Tree from String
- LeetCode 606 Construct String from Binary Tree
- LeetCode 606 Construct String from Binary Tree
- LeetCode Construct String from Binary Tree
- leetcode 606 Construct String from Binary Tree
- Construct String from Binary Tree(leetcode)
- c++面试题集锦
- 专利转让不活跃 我国专利运用能力有待提高
- 读取Properties文件的工具类
- 常见的20则正则表达式
- kivy的安装步骤
- leetcode 606. Construct String from Binary Tree
- nodejs npm常用命令
- 【LeetCode】2. Add Two Numbers
- 机器人领域的几大国际会议
- C语言字符串那些事儿
- 我的公司月流水2000万,但我却活得像条狗!
- js constructor
- JavaScript Promise理解
- 软件开发流程