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


相关推荐

  • 本地tomcat 配置环境变量[通俗易懂]

    本地tomcat 配置环境变量[通俗易懂]1、官网下载tomcat,并解压Tomcat官网2、找到tomcat解压路径,配置三个环境变量新建CATALINA_HOME环境变量,CATALINA_HOME=E:\tomcat\apache-tomcat-8.5.38新建CATALINA_BASE环境变量,CATALINA_BASE=E:\tomcat\apache-tomcat-8.5.38…

    2022年6月4日
    51
  • 评教,路上的风景更美「建议收藏」

    评教,路上的风景更美

    2022年1月30日
    33
  • linux系统卸载程序命令行,Linux系统中完全卸载删除程序的命令[通俗易懂]

    linux系统卸载程序命令行,Linux系统中完全卸载删除程序的命令[通俗易懂]如果您在数据中心服务器或本地服务器中使用Ubuntu或任何其他基于debian的发行版系统,您可能会遇到需要卸载软件的情况。一般情况下,您会登录并运行命令:sudoapt-getremovepackagename(其中packagename是要删除的程序名称)。但是,这样做会留下一些问题,比如安装依赖项和配置文件。这些分散的应用程序和文件不仅占用空间,而且可能导致安全问题。要完全删除的话怎么…

    2022年9月8日
    0
  • Visual C++ 2010 Express与Visual C++ 2010有何区别?「建议收藏」

    Visual C++ 2010 Express与Visual C++ 2010有何区别?

    2022年4月3日
    47
  • 动态规划之背包问题——01背包

    动态规划之背包问题——01背包文章目录一、01背包问题二、二维dp数组解决01背包问题1.确定dp数组以及下标的含义2.确定递推公式3.dp数组初始化4.确定遍历顺序5.举例推导dp数组三、一维dp数组解决01背包问题1.确定dp数组以及下标的含义2.一维dp数组的递推公式3.一维dp数组如何初始化4.一维dp数组遍历顺序5.举例推导dp数组四、leetcode例题讲解01背包问题416.分割等和子集1049.最后一块石头的重量II494.目标和474.一和零背包问题中我们常见的就是01背包和完全背包。在l

    2022年7月26日
    3
  • 排查挖矿病毒

    排查挖矿病毒场景最新发现linux服务器一直很卡,导致无法编译和其它相关操作。排查分析经top查看原来是一个叫269的进程一直抢占CPU,占比高达4000%。而该269进程则是挖矿病毒进行高度伪装,即使是kill掉该进程也无济于事,后面又会自动跑起来。top-19:29:19up1:24,2users,loadaverage:41.71,41.75,41.46Tasks:891total,3running,502sleeping,0st..

    2022年6月12日
    34

发表回复

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

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