[leetcode] 根据String数组构造TreeNode,用于LeetCode树结构相关的测试用例
LeetCode 跟树结构相关的题目的测试用例中大多是通过String数组来构造树。例如{2,#,3,#,4,#,5,#,6},可以构造出如下的树(将树结构逆时针选择90度显示):
6
5
4
3
2
很直观地可以理解,输入的String数组是对树结构进行“层序”遍历得到的结果。以下代码用于构造树结构,并提供printTree用于打印树结构。
package util; import java.util.LinkedList;
import java.util.Queue; public class util {
public static class TreeNode {
int val;
TreeNode left;
TreeNode right; TreeNode(int x) {
val = x;
}
} /*
* construct TreeNode from a array format string, for test cases of LeetCode
*/
public static TreeNode createTree(String tree) {
// {1,2,3,4,#,#,#,5,#,6,#,7,#,8}
String[] ss = tree.split(",");
return createTree(ss);
} public static TreeNode createTree(String[] tree) {
Queue<TreeNode> q = new LinkedList<TreeNode>();
// 1st one should not be #
TreeNode root = constructOne(tree[0]);
q.add(root);
int idx = 1;
while (!q.isEmpty()) { TreeNode tn = q.poll();
if (tn == null) {
continue;
}
// construct tn's left&right node
// when to stop
if (idx == tree.length) {
break;
}
TreeNode left_ = constructOne(tree[idx]);
tn.left = left_;
q.add(left_);
idx++;
if (idx == tree.length) {
break;
}
TreeNode right_ = constructOne(tree[idx]);
idx++; tn.right = right_;
// add to queue
q.add(right_);
} return root; } private static void printNode(TreeNode tn, int indent) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < indent; i++) {
sb.append("\t");
}
sb.append(tn.val);
System.out.println(sb.toString());
} public static void printTree(TreeNode root, int indent) {
if (root == null) {
return;
}
// if (root.left == null && root.right == null) {
// printNode(root, indent);
// }
// right
printTree(root.right, indent + 1);
// self
printNode(root, indent);
// left
printTree(root.left, indent + 1);
} public static void printTree(TreeNode root) {
// right first
printTree(root, 0);
} private static TreeNode constructOne(String s) {
if (s.compareTo("#") == 0) {
return null;
} else {
return new TreeNode(Integer.parseInt(s));
}
} public static void main(String args[]) {
TreeNode tn = createTree("2,#,3,#,4,#,5,#,6");
printTree(tn);
}
}
[leetcode] 根据String数组构造TreeNode,用于LeetCode树结构相关的测试用例的更多相关文章
- (string 数组) leetcode 804. Unique Morse Code Words
International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...
- [LeetCode] Interleaving String 交织相错的字符串
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...
- 【LeetCode题解】数组Array
1. 数组 直观地看,数组(Array)为一个二元组<index, value>的集合--对于每一个index,都有一个value与之对应.C语言中,以"连续的存储单元" ...
- [LeetCode] Repeated String Match 重复字符串匹配
Given two strings A and B, find the minimum number of times A has to be repeated such that B is a su ...
- [LeetCode] Construct String from Binary Tree 根据二叉树创建字符串
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
- C#LeetCode刷题-数组
数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...
- [LeetCode] Reverse String 翻转字符串
Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...
- [LeetCode] Magical String 神奇字符串
A magical string S consists of only '1' and '2' and obeys the following rules: The string S is magic ...
- [LeetCode] Reverse String II 翻转字符串之二
Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...
随机推荐
- Scala深入浅出实战经典-----002Scala函数定义、流程控制、异常处理入门实战
002-Scala函数定义.流程控制.异常处理入门实战 Scala函数定义 语句结束无分号 定义无参函数 def 函数名称(参数名称:参数类型)[:Unit=]{ 函数体 } 老师的代码 我的实际代码 ...
- 例子:Execution Model Sample - 应用状态保存
WP中,当你的应用被切换到后台 后,就进入了休眠状态,然后当一个应用从墓碑恢复时,如何恢复相应的状态,该例子就演示了如何保存和恢复UI以及APP相关状态. 这里有一篇很好的文章,请参见: http:/ ...
- cookies的获取,删除,设置
cookies,sessionStorage 和 localStorage 的区别? 1.cookie在浏览器和服务器间来回传递. sessionStorage和localStorage不会: 2.s ...
- JS 4 新特性:混合属性(mixins)
Ext JS4的新特征1:混合属性(mixins) 组合是Extjs4的新特性,可用于实现多继承的情况.该属性会以同步方式加载类文件,并实例化该类(译者推理其内部使用Ext.create方法).直接上 ...
- linux tar命令的使用
tar格式,会打包成一个文件,可以对多个目录,或者多个文件进行打包 tar命令只是打包,不会压缩,打包前后大小是一样的 tar命令 -c //打包 -x //解压 -f //指定文件 ...
- 用DropBox分享Unity3D的Web应用
用U3D做好游戏好想分享给亲朋好友体验怎么办?导出exe,apk都可以,只是下载始终是个门槛. 幸好还可以导出web版(虽然要安装unity3d的插件),但自己没有服务器怎么办,没关系~,现 在是云时 ...
- win7 32 bit VS2012 OpenCV3.0配置
今天看CPP基础,想起来之前在vs2012配置opencv3未成功,就忍不住再次配置一... 环境:win7 32bit vs2012 opencv3.0 主要参考这几篇博文:1,2,3 上面的博文已 ...
- 使用python编写一个壁纸网站的简单爬虫
目标网站:http://www.netbian.com/ 目的:实现对壁纸各分类的第一页壁纸的获取 一:分析网站,编写代码: (ps:源代码在文章的最后) 1.获取网站目录部分的一大段代码,下一步再进 ...
- UVA 572 油田连通块-并查集解决
题意:8个方向如果能够连成一块就算是一个连通块,求一共有几个连通块. 分析:网上的题解一般都是dfs,但是今天发现并查集也可以解决,为了方便我自己理解大神的模板,便尝试解这道题目,没想到过了... # ...
- [转]OOAD基本概念
转载地址:http://www.cnblogs.com/zfc2201/archive/2012/12/09/2810532.html 尊重原作者,转载请注明 学习目标: 1.理解与掌握面向对象的概念 ...