• [LeetCode] 606. Construct String from Binary Tree


    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.

    根据二叉树创建字符串。题意是将给的二叉树按照前序遍历的顺序序列化。

    这道题既然说了是按照前序遍历的顺序序列化,那么思路也就是前序遍历。我们需要一个stringbuilder和一个helper函数。唯一需要注意的地方是如果某个节点没有左孩子但是有右孩子(39行),需要记得创建一个空的括号对否则没有左孩子这个信息是无法被表示的。

    时间O(n)

    空间O(n)

    Java实现

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

    相关题目

    297. Serialize and Deserialize Binary Tree

    449. Serialize and Deserialize BST

    536. Construct Binary Tree from String

    606. Construct String from Binary Tree

    LeetCode 题目总结

  • 相关阅读:
    IIS 下配置无后缀的URL ReWrite
    获得代理IP或客户端Ip
    asp.net发送邮箱
    URLRewrite伪静态与AspNetPager分页控件的结合
    邮箱大全
    注册表单验证正则表达式
    aspx中伪静态的实现
    JMail发送邮件
    IE 6中负的margin值导致出界部分不显示问题的解决
    Culminis为每个用户组提供的免费TechNet Plus Direct订阅
  • 原文地址:https://www.cnblogs.com/cnoodle/p/13850101.html
Copyright © 2020-2023  润新知