606. 根据二叉树创建字符串

你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串。

空节点则用一对空括号 “()” 表示。而且你需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空括号对。

示例 1:

输入: 二叉树: [1,2,3,4]

       1
/ \
2 3
/
4

输出: “1(2(4))(3)”

解释: 原本将是“1(2(4)())(3())”,

在你省略所有不必要的空括号对之后,

它将是“1(2(4))(3)”。

示例 2:

输入: 二叉树: [1,2,3,null,4]

   1
/ \
2 3
\
4

输出: “1(2()(4))(3)”

解释: 和第一个示例相似,

除了我们不能省略第一个对括号来中断输入和输出之间的一对一映射关系。

PS:

遍历树

/**
* 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();
doTree2str(t, sb);
return sb.toString();
} private void doTree2str(TreeNode t, StringBuilder sb) {
if (t != null) {
sb.append(t.val);
if (t.left != null || t.right != null) {
sb.append('(');
doTree2str(t.left, sb);
sb.append(')');
if (t.right != null) {
sb.append('(');
doTree2str(t.right, sb);
sb.append(')');
}
}
}
}
}

Java实现 LeetCode 606 根据二叉树创建字符串(遍历树)的更多相关文章

  1. Leetcode 606. 根据二叉树创建字符串

    题目链接 https://leetcode.com/problems/construct-string-from-binary-tree/description/ 题目描述 你需要采用前序遍历的方式, ...

  2. LeetCode 606. Construct String from Binary Tree根据二叉树创建字符串 (C++)

    题目: You need to construct a string consists of parenthesis and integers from a binary tree with the ...

  3. leadcode的Hot100系列--二叉树创建和遍历

    很多题目涉及到二叉树,所以先把二叉树的一些基本的创建和遍历写一下,方便之后的本地代码调试. 为了方便,这里使用的数据为char类型数值,初始化数据使用一个数组. 因为这些东西比较简单,这里就不做过多详 ...

  4. Leetcode题目102.二叉树的层次遍历(队列-中等)

    题目描述: 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 ...

  5. Python -二叉树 创建与遍历算法(很详细)

    树表示由边连接的节点.它是一个非线性的数据结构.它具有以下特性. 一个节点被标记为根节点. 除根节点之外的每个节点都与一个父节点关联. 每个节点可以有一个arbiatry编号的chid节点. 我们使用 ...

  6. 【LeetCode】144. 二叉树的前序遍历

    144. 二叉树的前序遍历 知识点:二叉树:递归:Morris遍历 题目描述 给你二叉树的根节点 root ,返回它节点值的 前序 遍历. 示例 输入:root = [1,null,2,3] 输出:[ ...

  7. [LeetCode] Construct String from Binary Tree 根据二叉树创建字符串

    You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...

  8. [Swift]LeetCode606. 根据二叉树创建字符串 | Construct String from Binary Tree

    You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...

  9. Q606 根据二叉树创建字符串

    你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串. 空节点则用一对空括号 "()" 表示.而且你需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空 ...

随机推荐

  1. train loss与test loss结果分析/loss不下降

    train loss与test loss结果分析 train loss 不断下降,test loss不断下降,说明网络仍在学习; train loss 不断下降,test loss趋于不变,说明网络过 ...

  2. 关于网上quartus ii 生成fft核出现问题解决

    ------------恢复内容开始------------ 关于网上quartus ii 生成fft核出现问题解决 1:必须把软件破解啦 2:必须把IP核破解啦 破解步骤网上也有可以直接看,一定要全 ...

  3. 一文教你如何在ubuntu上快速搭建STM32 CubeIDE环境(图文超详细+文末有附件)

    在快速ubuntu上安装cubeide你值得拥有:适合对linux系统还不是很熟悉的同学: 文章目录 1 下载 cubeide 2 找到软件 3 安装 4 附件 5 总结 1 下载 cubeide 登 ...

  4. springboot controller templates html

    首先声明: @Controller注解的类必须要在启动类的子集目录下,否则无法扫描 本文要求: 通过controller层跳转页面到html页面(本篇用到thymeleaf模板) 项目结构展示: 第一 ...

  5. Velocity中判断表达式是不是为空

    Velocity中判断表达式是不是为空 $if ($null.isNull($mycoll) || $mycoll.size()==0) ${}与$!{}区别 例子: ${str}:如果str没有值, ...

  6. Codeforces1176A(A题)Divide it!

    Divide it! You are given an integer nn. You can perform any of the following operations with this nu ...

  7. MySQL事务及实现、隔离级别及锁与优化

    事务  事务是应用程序中一系列严密的操作,所有操作必须成功完成,否则在每个操作中所作的所有更改都会被撤消.事务是逻辑上的一组操作,要么都执行,要么都不执行. ACID简介 原子性(Atomicity) ...

  8. java 精确加减

    /** * 提供精确的加法运算. * @param v1 被加数 * @param v2 加数 * @return 两个参数的和 */ public double add(double v1, dou ...

  9. UVA 11651

    题目链接:https://cn.vjudge.net/problem/UVA-11651 解题思路: 思路来源于网络. DP + 矩阵快速幂. 设 dp[i][j] 为满足 score 为 i 且最后 ...

  10. Htop/Glances/Dstat性能测试系统监控工具领域的瑞士军刀

    原文链接:https://mp.weixin.qq.com/s/TvfzIy4uXHPOFQ1h5Q4KWg 建议点击原文链接查看 续上篇分享的[性能测试工具],今天整理了常用的系统监控工具,当然有特 ...