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


相关推荐

  • java缓存数据并配置有效时间[通俗易懂]

    java缓存数据并配置有效时间[通俗易懂]没有用到redis只是单纯的使用内存存储数据实现的功能:缓存数据并配置有效时间,可设置默认时间自动清除缓存,也可以自己设置。直接上代码:importjava.util.LinkedList;importjava.util.List;importjava.util.Map.Entry;importjava.util.Timer;importjava.util.TimerTask;importjava.util.concurrent.ConcurrentHashMap;publ

    2022年10月4日
    3
  • Windows 软RAID 1操作教程[通俗易懂]

    Windows 软RAID 1操作教程[通俗易懂]  文章原始地址:http://feotech.com/?p=190  本文将介绍基于Windows操作系统的软RAID1的创建于更换磁盘的操作方法,关于每种RAID的各自原理请点击以下Wikipedia链接查看https://en.wikipedia.org/wiki/Standard_RAID_levelsRAID1的特点是将相同数据同时写入2块硬盘中,起到数据…

    2022年7月15日
    105
  • http://www.cnbc.com/2016/07/12/tensions-in-south-china-sea-to-persist-even-after-court-ruling.html「建议收藏」

    http://www.cnbc.com/2016/07/12/tensions-in-south-china-sea-to-persist-even-after-court-ruling.html「建议收藏」http://www.cnbc.com/2016/07/12/tensions-in-south-china-sea-to-persist-even-after-court-ruling.htmlT

    2022年7月3日
    26
  • RBF神经网络实验原理_神经网络多元拟合

    RBF神经网络实验原理_神经网络多元拟合RBF神经网络及拟合实例RBF神经网络介绍RBF神经网络结构RBF神经网络算法RBF神经网络逼近算法采用RBF神经网络逼近非线性函数神经网络逼近结果代码如下RBF神经网络介绍RBF神经网络结构径向基函数(RadialBasisFunction,RBF)神经网络是一种单隐含层的三层前馈神经网络,网络结构如下图所示RBF神经网络模拟了人脑中局部调整,相互覆盖接受域(或者说感受域,ReceptiveField)的神经网络结构。与BP神经网络相同,研究人员已经证明RBF神经网络能够以任何精度逼近任

    2025年6月1日
    2
  • bi报表开发工具_三大报表的勾稽关系图

    bi报表开发工具_三大报表的勾稽关系图为什么需要电子表格国内目前的同类产品中都有报表工具,这些工具大部分都有一个类似Excel的操作界面:单元格、快捷键、工具栏等典型设计工具要求。这些工具要么需要有专业的背景,或者专业的工程师提供支持,要么学习成本高,调整报表样式十分麻烦。作为报表开发人员而言,花费大量时间去学习一个新工具是一件非常苦恼的事情,我们能否直接把exce作为报表设计的工具呢?基于这个思路,诞生了我们的Spreadsheet…

    2022年10月19日
    2
  • minhash算法_verlet算法

    minhash算法_verlet算法背景如何设计一个比较两篇文章相似度的算法?可能你会回答几个比较传统点的思路:一种方案是先将两篇文章分别进行分词,得到一系列特征向量,然后计算特征向量之间的距离(可以计算它们之间的欧氏距离、海明距离或者夹角余弦等等),从而通过距离的大小来判断两篇文章的相似度。另外一种方案是传统hash,我们考虑为每一个web文档通过hash的方式生成一个指纹(fingerprint)。下面,我们来分析下这两种方法…

    2022年10月1日
    2

发表回复

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

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