LeetCode 606. Construct String from Binary Tree「建议收藏」

LeetCode 606. Construct String from Binary Tree

大家好,又见面了,我是你们的朋友全栈君。

question:

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.

 

分析:

先根遍历

try:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public String tree2str(TreeNode t) {
        StringBuilder sb = new StringBuilder();
        pre_orderVisit(t,sb);
        return sb.toString();
    }
    
    public void pre_orderVisit(TreeNode t,StringBuilder sb){
        if(t==null){
            return;
        }
        sb.append(t.val);
        if(t.left==null&&t.right==null){
            return;
        }
        sb.append("(");
        pre_orderVisit(t.left,sb);
        sb.append(")");
        if(t.right!=null){
            sb.append("(");
            pre_orderVisit(t.right,sb);
            sb.append(")");
        }
        
    }
    
}

 

result:

LeetCode 606. Construct String from Binary Tree「建议收藏」

 

conclusion:

 

转载于:https://www.cnblogs.com/hzg1981/p/8970387.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/107553.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • creating server tcp listening socket 127.0.0.1:6379: bind No error

    creating server tcp listening socket 127.0.0.1:6379: bind No errorwindow下启动redis服务报错:creatingservertcplisteningsocket127.0.0.1:6379:bindNoerror的解决方案如下按顺序输入如下命令就可以连接成功redis-cli.exeshutdownexitredis-server.exeredis.windows.conf参考连接:https://blog.csdn….

    2022年5月2日
  • (36)JS运动之使物体向右运动「建议收藏」

    (36)JS运动之使物体向右运动

    2022年1月30日
  • 低延迟视频传输_网络延时

    低延迟视频传输_网络延时微信后台如何应对像跨年,特殊时刻(比如2022年2月22日22时22分22秒)这样的朋友圈突发流量,可做如下策略(只是比如):优先让发一张图片的成功。九宫格只成功一部分,剩余的强有损压缩。朋友多的优先成功。(也可以反过来)设置三天可见的优先成功。(也可以反过来)年轻女性优先成功。(这个没毛病)历史点赞多的优先成功。(也可以反过来)头像穿西装打领带的经理统统失败。…在不影响P99用户体验的前提下,提供有损服务,保证核心可用性,这就是柔性。但数据传输的思维定势并不认可柔性。受TCP的影

    2022年10月3日
  • vim安装YouCompleteMe 插件

    vim安装YouCompleteMe 插件vim安装youcomplete插件,折磨我好久,装不好硬是要把它装好,结果最后终于装好这个补全的插件。为了需要安装的人少走弯路,我的安装过程如下(主要提供步骤):1、先看你的vim版本是多少。我的是vim7.4.52貌似支持python。但是还是不行说是不支持YCM。 最后只能安装vim8.0,手动安装。 具体可以vim–version看来下python和python3是否

    2022年11月2日
  • epub精品电子书下载_丹尼尔·内格里诺

    epub精品电子书下载_丹尼尔·内格里诺郑重声明:本号收录的电子书均来源于互联网或网友分享,链接内容仅作分享交流学习使用,不用于任何商业用途,版权归原作者和出版社所有,如果喜欢,请支持和购买正版,谢谢。下载地址:http://pan.63

    2022年8月3日
  • mac datagrip2021.4激活码_通用破解码

    mac datagrip2021.4激活码_通用破解码,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月16日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号