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)
上一篇 2022年4月2日 下午5:00
下一篇 2022年4月2日 下午5:35


相关推荐

  • 客户端是移动端吗_谈单的技巧

    客户端是移动端吗_谈单的技巧1、百度开放平台选择开发者服务申请应用2、zai

    2022年10月8日
    6
  • 用c++和SFML实现简易的界面版贪吃蛇

    用c++和SFML实现简易的界面版贪吃蛇运行截图等待开始界面运行过程失败界面截图 SFML 配置 csdn 上面已经有很多 SFML 配置的 blog 随便就能搜到 正常配置好 SFML 后 还需要将字体 ttf 文件放在源代码同一目录和 exe 同一目录中 不然无法显示字符代码部分下面贴上各个部分的代码头文件和全局常量 include SFML Graphics hpp include time h include iostream include iostream time h SFML

    2026年3月18日
    2
  • pendingIntent初步_什么是pendingIntent「建议收藏」

    pendingIntent字面意义:等待的,未决定的Intent。 要得到一个pendingIntent对象,使用方法类的静态方法 通过getActivity(Context context, int requestCode, Intent intent, int flags)从系统取得一个用于启动一个Activity的PendingIntent对象,通过getService(Co

    2022年3月9日
    58
  • PID控制原理详解(一)[通俗易懂]

    PID控制原理详解(一)[通俗易懂]PID的理解       关于理解PID控制算法最典型的一个例子就是一个漏水的水缸的问题。网上有很多讲解PID的帖子会讲到这个例子。这里我也把我自己对于PID的理解用这个例子阐述一遍。       有个漏水的水缸,而且漏水的速度还不是恒定的。然后我们还有个水桶,我们可以控制往水缸里面加水或者从水缸里面舀水出来。另外我们可以检测水平面。现在我们的目的就是要控制水平面稳定在我们想要的任何一个平面上…

    2022年5月20日
    43
  • 中国石化测试面试题_中石化面试一般问什么

    中国石化测试面试题_中石化面试一般问什么面试过程:首先,上午进行面试人员签到,大约100人左右。一共要2个人。下午1点半开始统一面试。人员较多,所以每个人只有3分钟时间,一共最少8位面试官。过程中,他们很少提问题,如果你的技术比较新颖,会问你一些。例如SSH或SSM框架就没意思了。面试官问的面试题:以下都是对我当时的提问及个人回答。1.你都擅长哪些RPC技术。答:webservice或者restFul或者ICE微服务。2.你用过微服务…

    2022年10月15日
    3
  • 决心书

    决心书

    2022年3月6日
    46

发表回复

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

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