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     }
  • 相关阅读:
    phonegap android 输入法弹出会遮盖表单框的解决办法
    html5 webDatabase 存储中sql语句执行可嵌套使用
    Spring @Cacheable注解无法生效或不起作用的问题整理
    SpringBoot---缓存技术2(基于Redis注解和基于RedisTemplate的手动缓存,包括将缓存数据序列化成json数据,有源码)
    使用document.domain+iframe跨域实例
    为已创建的pgsql设置主键自增
    基于canvas的流程编辑器
    使用RedisTemplate缓存List的一系列操作示例
    kafka
    element-ui table 剧中
  • 原文地址:https://www.cnblogs.com/abc-begin/p/7582059.html
Copyright © 2011-2022 走看看