1. 程式人生 > >606. Construct String from Binary Tree 【easy】

606. Construct String from Binary Tree 【easy】

new mos pen you image color emp same exce

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的代碼:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 string tree2str(TreeNode* t) { 13 string result; 14 15 if (t == NULL) { 16 return ""; 17 } 18 19 result += to_string(t->val); 20 21 if (t->left) { 22 result += "(" + tree2str(t->left) + ")"; 23 } 24 25 if (t->right) { 26 result += "(" + tree2str(t->right) + ")"; 27 } 28 29 return result; 30 } 31 };

技術分享

實際上對應的圖如下圖,可以發現需要對左子樹為空,右子樹不為空的情況做個特殊判斷。

技術分享

解法一:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     string tree2str(TreeNode* t) {
13         string result;
14         
15         if (t == NULL) {
16             return "";
17         }
18         
19         result += to_string(t->val);
20         
21         if (t->left) {
22             result += "(" + tree2str(t->left) + ")";
23         }
24         else if (t->right) {
25             result += "()";
26         }
27         
28         if (t->right) {
29             result += "(" + tree2str(t->right) + ")";   
30         }
31         
32         return result;
33     }
34 };

上面代碼中第24 ~ 26行就是對左子樹為空,右子樹不為空的情況做的特殊判斷。

解法二:

1 class Solution {
2 public:
3     string tree2str(TreeNode* t) {
4         return !t ? "" : to_string(t->val) + (t->left ? "(" + tree2str(t->left) + ")" : t->right ? "()" : "")
5                                            + (t->right ? "(" + tree2str(t->right) + ")" : "");
6     }
7 };

參考@alexander 的代碼。

解法三:

 1 public class Solution {
 2     public String tree2str(TreeNode t) {
 3         if (t == null) return "";
 4         
 5         String result = t.val + "";
 6         
 7         String left = tree2str(t.left);
 8         String right = tree2str(t.right);
 9         
10         if (left == "" && right == "") return result;
11         if (left == "") return result + "()" + "(" + right + ")";
12         if (right == "") return result + "(" + left + ")";
13         return result + "(" + left + ")" + "(" + right + ")";
14     }
15 }

最後集中起來再判斷的思路很好,參考@shawngao 的代碼。

解法四:

 1 public String tree2str(TreeNode t) {
 2         StringBuilder sb = new StringBuilder();
 3         helper(sb, t);
 4         return sb.toString();
 5     }
 6     public void helper(StringBuilder sb, TreeNode t) {
 7         if (t != null) {
 8             sb.append(t.val);
 9 
10             if(t.left != null || t.right != null) {
11                 sb.append("(");
12                 helper(sb, t.left);
13                 sb.append(")");
14 
15                 if(t.right != null) {
16                     sb.append("(");
17                     helper(sb, t.right);
18                     sb.append(")");
19                 }
20             }
21         }
22     }

606. Construct String from Binary Tree 【easy】