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

 

 


 题目标签:Tree
  这道题目给了我们一个二叉树,需要我们建立一个二叉树结构的string。并且要省去一些不需要的括号组合。比如一个node只有left,没有right,那么这个right的()就是不需要的;又或者一个node,既没有左边也没有右边,那么这时候left和right的()都不需要。
那么就一个点,有4种可能性:
1- 没有left, 也没有right: 直接返回它自己的值;
2- 有left, 也有right: 那么left 和 right 都需要括号,并且把left 和 right recursively 代入function;
3- 没有left, 但有right: 那么left 和 right 都需要括号,并且把right recursively 代入function;
4- 有left, 但没有right: 那么只有left 需要括号, 并且把left recursively 代入function。
 
 

Java Solution:

Runtime beats 53.70% 

完成日期:06/29/2017

关键词:Tree

关键点:分析可能性,利用条件来控制括号

 
 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution 
11 {
12     public String tree2str(TreeNode t) 
13     {
14         String res = "";
15         
16         if(t == null)
17             return res;
18         
19         if(t.left == null && t.right == null)
20             return res + t.val;
21         
22         res += t.val;
23         
24         // case 1: left is not null, right is not null;
25         if(t.left != null && t.right != null)
26         {
27             // left child
28             res += "(" + tree2str(t.left) + ")";
29             // right child
30             res += "(" + tree2str(t.right) + ")";
31         }
32         else if(t.left == null && t.right != null) // case 2: left is null, right is not null;
33         {
34             // left child
35             res += "(" + ")";
36             // right child
37             res += "(" + tree2str(t.right) + ")";
38         }
39         else if(t.left != null && t.right == null) // case 3: left is not null, right is null;
40         {
41             // left child
42             res += "(" + tree2str(t.left) + ")";
43         }
44         
45             
46         return res;
47     }
48 }

参考资料:N/A

 

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

 
posted @ 2017-06-30 09:54  Jimmy_Cheng  阅读(349)  评论(0编辑  收藏  举报