zoukankan      html  css  js  c++  java
  • [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 题目总结

  • 相关阅读:
    使用getopts处理Shell脚本参数
    SAP R/3 MM模块学习笔记
    应 阿成1 要求 co主要业务操作手册
    AIX磁盘管理命令
    工作中心和工艺路线
    顾问学院培训教材 TAMM ,TAPP,TASD,TACO
    十个“三角形”汉字,好看、不好认
    ALV做出的报表里更改布局里没有保存按钮的解决方法
    SAP系统内的发票校验
    修改SAP 登录后的背景图片
  • 原文地址:https://www.cnblogs.com/cnoodle/p/13850101.html
Copyright © 2011-2022 走看看