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


相关推荐

  • gdp是什么意思_dpd30+什么意思

    gdp是什么意思_dpd30+什么意思%~dp0表示批处理文件所在的路径只能用在批处理文件中

    2025年11月25日
    4
  • MySQL安装(详细,适合小白)

    MySQL安装(详细,适合小白)MySQL安装一、mysql安装包下载二、配置my.ini文件三、初始化MySQL四、可能遇到的错误操作一、mysql安装包下载官网下载地址:mysql安装包下载如图所示:二、配置my.ini文件解压后的文件尽量不要放在C盘(内存小),解压后如下图所示在上图所示梗目录下配置my.ini文件[mysqld]#设置3306端口port=3306[mysqld]#设置3306端口port=3306#设置mysql的安装目录(存放地址可以更改)basedir=E:\My

    2022年6月6日
    43
  • 前端开发写代码哪个软件更好用?

    前端开发写代码哪个软件更好用?群里的朋友,经常问到web前端开发写代码用那个软件好?今天在这里统一回答下,主流的web前端开发写代码的软件有这些Webstorm、Vscode、SublimeText、HBuilder、Dreamweaver、notepad++、editplus等,做前端这么多年了,下面谈下我的使用感受吧。1.WebStorm【推荐】WebStorm是jetbrai…

    2022年5月30日
    39
  • laravel 授权使用gate门类

    laravel 授权使用gate门类

    2021年10月24日
    40
  • C#基础学习之——(一)Dock与Anchor

    C#基础学习之——(一)Dock与Anchor提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档文章目录前言一、Dock与Anchor是什么?1.Dock2.Anchor二、使用步骤1.设计界面2.使用Anchor与Dock总结前言C#基础学习之——(一)Dock与Anchor最近在对窗体控件进行布局时,发现了Dock与Anchor这两种不同的窗体布局属性,所以查阅了一些资料,在这里进行记录。提示:以下是本篇文章正文内容,下面案例可供参考一、Dock与Anchor是什么?1.Dock①Dock在英文中是停泊的意

    2025年9月4日
    8
  • jmeter+ant+jenkins实现自动化接口集成测试框架下载,build.xml下载

    jmeter+ant+jenkins实现自动化接口集成测试框架下载,build.xml下载

    2021年9月18日
    45

发表回复

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

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