Leetcode 606. 根据二叉树创建字符串
题目链接
https://leetcode.com/problems/construct-string-from-binary-tree/description/
题目描述
你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串。
空节点则用一对空括号 "()" 表示。而且你需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空括号对。
示例 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)"
解释: 和第一个示例相似,
除了我们不能省略第一个对括号来中断输入和输出之间的一对一映射关系。
题解
代码
/**
* 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();
backTrack(t, sb);
return sb.toString();
}
public void backTrack(TreeNode t, StringBuilder sb) {
if (t == null) { return ; }
sb.append(t.val);
if (t.left != null || t.right != null) {
sb.append("(");
backTrack(t.left, sb);
sb.append(")");
}
if (t.right != null) {
sb.append("(");
backTrack(t.right, sb);
sb.append(")");
}
}
}
Leetcode 606. 根据二叉树创建字符串的更多相关文章
- Java实现 LeetCode 606 根据二叉树创建字符串(遍历树)
606. 根据二叉树创建字符串 你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串. 空节点则用一对空括号 "()" 表示.而且你需要省略所有不影响字符串与原 ...
- 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 ...
- [LeetCode] Construct String from Binary Tree 根据二叉树创建字符串
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
- [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 ...
- Q606 根据二叉树创建字符串
你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串. 空节点则用一对空括号 "()" 表示.而且你需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空 ...
- Leetcode606.Construct String from Binary Tree根据二叉树创建字符串
你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串. 空节点则用一对空括号 "()" 表示.而且你需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空 ...
- 【刷题笔记】LeetCode 606. Construct String from Binary Tree
题意 给一棵二叉树,把它转化为字符串返回.转化字符串的要求如下: 1. null 直接转化为 () ;(这个要求其实有点误导人~) 2. 子节点用 () 包裹起来:(这是我自己根据例子添加的要求) ...
- 【python】Leetcode每日一题-扰乱字符串
[python]Leetcode每日一题-扰乱字符串 [题目描述] 使用下面描述的算法可以扰乱字符串 s 得到字符串 t : 如果字符串的长度为 1 ,算法停止 如果字符串的长度 > 1 ,执行 ...
- java中创建字符串的两种方式(“”与new String())及区别
结论:通过""创建的字符串实际上在java堆中只有一个,而通过new string创建出来的字符串在java堆中占有不同的内存. 第一个True表明这两个在内存中拥有相同的地址,那 ...
随机推荐
- jQuery设置全选和全反选
HTML 代码: <input type="checkbox" id="allChecked" onclick="setAllChecked(t ...
- Vue.js 插件开发
Vue.js 的插件应当有一个公开方法 install .这个方法的第一个参数是 Vue 构造器 , 第二个参数是一个可选的选项对象: MyPlugin.install = function (Vue ...
- css取消a标签在移动端点击时的背景颜色
一.取消a标签在移动端点击时的蓝色 -webkit-tap-highlight-color: rgba(255, 255, 255, 0);-webkit-user-select: none;-moz ...
- Java设计模式—中介者模式
中介者模式是一种并不常用的模式,在此简单阐述阐述. 定义:用一个中介者对象封装一系列的对象交互,中介者使各对象不需要显示地相互作用,从而使耦合松散,而且可以独立地改变它们之间的交互. 类型:行为类模式 ...
- <Android 基础(九)> Ndk配置与Demo
介绍 The NDK is a toolset that allows you to implement parts of your app using native-code languages s ...
- X-Cart-5.3.1.4 (Ubuntu 16.04)
平台: Ubuntu 类型: 虚拟机镜像 软件包: x-cart-5.3.1.4 commercial ecommerce open-source x-cart 服务优惠价: 按服务商许可协议 云服务 ...
- check_mk插件 redis
/usr/lib/check_mk_agent/plugins/mk_redis #!/bin/bash echo '<<<redis>>>' hosts=$(ne ...
- 谁动了我的I/O?
首先,是信用卡账单欠款0.13美刀~~~然后上亚马逊云查了一下账单. 3M次I/O...(1215133次超额的,2000000次免费的.) 于是监控了一下数据:每秒至少写5次,每秒写300KB,平均 ...
- 51nod 1366 贫富差距
题目来源: TopCoder 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 一个国家有N个公民,标记为0,1,2,...,N-1,每个公民有一个存款额.已知每个公 ...
- April 15 2017 Week 15 Saturday
Attitude is a little thing that makes a big difference. 小态度,大不同. Attitudes can make a big difference ...