zoukankan      html  css  js  c++  java
  • 606. Construct String from Binary Tree 【easy】

    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     }
  • 相关阅读:
    个人应该做什么样的网站?
    得对xls文件作操作
    正则替换
    31 反馈的判断方法(2)
    AD 层次原理图设计 自上而下
    27 多级放大电路的频率响应与第四章集成运放的电流源电路
    P28 集成运放的电流源电路(2)
    32 负反馈放大电路的方块图
    ESP8266固件AT指令连接EMQ 的MQTT服务器
    33 深度负反馈的实质与放大倍数分析
  • 原文地址:https://www.cnblogs.com/abc-begin/p/7582059.html
Copyright © 2011-2022 走看看