• 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     }
  • 相关阅读:
    Java Web表达式注入
    Mysql:The Federated Storage Engine:联合(联盟)存储引擎:【远程连接】服务器引擎
    Mysql:16.11.1 Pluggable Storage Engine Architecture
    Mysql:Server Logs:mysqld服务器的各种日志
    Mysql:InnoDB Table Compression and InnoDB Page Compression:适用于InnoDB的:表压缩 & 页压缩
    Mysql:Redo Log
    kubectl相关指令
    使用alias简化kubectl输入
    使用Minikube运行一个本地单节点Kubernetes集群(阿里云)
    Javascript去掉base64中的回车换行
  • 原文地址:https://www.cnblogs.com/abc-begin/p/7582059.html
Copyright © 2020-2023  润新知