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);
}
}

最新文章

  1. 6. web前端开发分享-css,js移动篇
  2. jQuery对话框右上角带关闭&#215;
  3. asp.net关于如何准许api跨域访问
  4. Hadoop实战5:MapReduce编程-WordCount统计单词个数-eclipse-java-windows环境
  5. C++ Primer----智能指针类 2
  6. 修改 上传图片按钮input-file样式。。
  7. phpcms 04
  8. IO流 总结一
  9. 转 【O2O案例】汽车后市场垂直化电子商务:平业模式解析
  10. [@Controller]3 详解@CookieValue,@PathVariable,@RequestBody,@RequestHeader,@RequestParam
  11. hdu2289Cup(神坑题,精度+二分,以半径二分不能过,以高度为二分就过了)
  12. LED恒流驱动IC汇总
  13. NC 6系后台调用接口保存单据
  14. Landen邀请码
  15. BZOJ4475 [Jsoi2015]子集选取
  16. LeetCode——Product of Array Except Self
  17. http://www.atool.org/keytype.php#0-tsina-1-53371-397232819ff9a47a7b7e80a40613cfe1
  18. varchar(n)跟varchar(max)的区别
  19. 第八篇:Spark SQL Catalyst源码分析之UDF
  20. mysqldumpl备份

热门文章

  1. red hat enterprise 6安装tftp服务
  2. Pegasos: Primal Estimated sub-GrAdient Solver for SVM
  3. HTML5 &lt;video&gt; - 使用 DOM 进行控制
  4. c++程序员必知的几个库
  5. c# txt文件的读写
  6. 填补Resources和WWW加载本地资源的坑
  7. MFC对话框显示BMP图片
  8. CxImage图像库的使用 .
  9. jquery jqPlot API 中文使用教程
  10. 关于thinkphp中的G方法使用