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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 六周第四次课(11月23日) 复习 扩展

    六周第四次课(11月23日) 复习 扩展

    2022年3月12日
    34
  • 图片怎么存储到数据库里「建议收藏」

    我们存储图片到数据库里一般有两种方式将图片保存的路径存储到数据库(文件路径或者ftp路径)将图片以二进制数据流的形式直接写入数据库字段中(base64)FTP:FTP服务器(FileTransferProtocolServer)是在互联网上提供文件存储和访问服务的计算机,它们依照FTP协议提供服务。FTP是FileTransferProtocol(文件传输协议)。顾名思义,就是专门用来传输文件的协议。简单地说,支持FTP协议的服务器就是FTP服务器。关于图片或者文件在数据库.

    2022年4月10日
    34
  • ecilpse运行Tomcat显示端口被占用Several ports (8005, 8080, 8009)「建议收藏」

    ecilpse运行Tomcat显示端口被占用Several ports (8005, 8080, 8009)「建议收藏」ecilpse运行Tomcat显示端口被占用Several ports (8005, 8080, 8009)

    2022年4月24日
    45
  • oh my zsh配置_setlanguage?lang=classic-zh-cn

    oh my zsh配置_setlanguage?lang=classic-zh-cn什么是Shell?相对于内核来说,Shell是Linux/Unix的一个外壳,它负责外界与Linux内核的交互,接收用户或其他应用程序的命令,然后把这些命令转化成内核能理解的语言,传给内核,内核是真

    2022年8月5日
    3
  • vim补全插件_vim实用插件

    vim补全插件_vim实用插件在PyCharm中安装Vim插件ideavim进入File菜单下的Settings下的Plugins,搜索ideaVim找到ideaVim插件点击Install安装重启并享受在Pycharm环境中使用Vim的乐趣,支持Vim三种模式的大部分命令下面可以不用设置:现在又有一个问题来了,重启后进入vim模式下,但是在vim模式下我们想要从Pycharm编辑区复制代码到别的…

    2022年8月28日
    3
  • 大一新生应该如何学习C语言,书上代码看不懂理解不了怎么办?

    大一新生应该如何学习C语言,书上代码看不懂理解不了怎么办?大家好,我是二哥呀!昨天有个读者问我要C语言的学习路线,他今年刚上大一,书上的代码完全看不懂。讲真,大一新生,一般都是零基础的纯小白,看不懂书上的代码很正常,除非是小学、初中、高中就开始卷计算机的硬核少年;或者是因为教材选的有问题。那刚好二哥之前整理过一些学习C语言的资料和学习方法,今天趁这个机会就再做个汇总和梳理。推荐一本书,两门视频课,若干学习建议,看完后如果还看不懂、理解不了C语言,过来骂我、捶我,只要不要打脸就行。01)阮一峰老师的C语言入门教程这个教程是开源的,采用知识共享许可

    2022年6月11日
    33

发表回复

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

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