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);    }}

原创粉丝点击