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


相关推荐

  • axios安装与基本方法

    axios安装与基本方法安装:1.npm安装:npminstallaxios2.在主入口文件main.js中引用:importaxiosfrom’axios’Vue.use(axios);3.在组件文件中的methods里使用:getNewsList(){this.axios.get(‘api/getNewsList’).then((response)=>{this.newsList=response.data.data;}).cat

    2025年6月1日
    2
  • 一个反射型XSS例子的解析

    一个反射型XSS例子的解析我们在访问一个网页的时候,在URL后面加上参数,服务器根据请求的参数值构造不同的HTML返回。如http://localhost:8080/prjWebSec/xss/reflectedXSS.jsp?param=value…上例中的value可能出现在返回的HTML(可能是JS,HTML某元素的内容或者属性)中,如果将value改成可以在浏览器中被解释执行的东西,就形成了反射型X

    2022年5月5日
    91
  • ▲ Android 动画望远镜效果

    ▲ Android 动画望远镜效果

    2021年3月12日
    150
  • 贪吃蛇电脑代码能直接玩_贪吃蛇为什么能安装不能玩

    贪吃蛇电脑代码能直接玩_贪吃蛇为什么能安装不能玩贪吃蛇无敌版,可穿墙,英文输入法小写字母wasd操作。

    2025年9月13日
    6
  • 矩阵的计算[通俗易懂]

    矩阵的计算[通俗易懂]矩阵运算规则:矩阵与常量运算矩阵与向量运算矩阵与矩阵运算矩阵之间相乘,必须满足B矩阵列数等于A矩阵行数才能运算,矩阵与矩阵之间的计算可以拆分为矩阵与多个向量的计算再将结果组合,返回的结果为一个列数等于B矩阵、行数等于A矩阵的矩阵。矩阵加减矩阵加减必须满足矩阵之间纬度相同,返回的结果也会是一个相同纬度的矩阵。矩阵的乘法规律:不满足交换律,A×B≠B×A满足结合律,A×(B×C)=(A×B)×C满足分配率,A×(B+C)=A×B.

    2022年4月19日
    62
  • 查看服务器的外网IP

    查看服务器的外网IP查看服务器的外网IP1、curlcip.cc[test@rabbitmq02~]$curlcip.ccIP :220.168.33.22地址 :中国湖南长沙运营商 :电信数据二 :湖南省长沙市|电信数据三 :URL :http://www.cip.cc/220.168.33.222、curlmyip.ipip.net[test@rabbitmq02~]$curlmyip.ipip.net当前IP:220.168.33.22来自于:中

    2022年5月13日
    84

发表回复

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

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