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树结构相关的测试用例的更多相关文章

  1. (string 数组) leetcode 804. Unique Morse Code Words

    International Morse Code defines a standard encoding where each letter is mapped to a series of dots ...

  2. [LeetCode] Interleaving String 交织相错的字符串

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...

  3. 【LeetCode题解】数组Array

    1. 数组 直观地看,数组(Array)为一个二元组<index, value>的集合--对于每一个index,都有一个value与之对应.C语言中,以"连续的存储单元" ...

  4. [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 ...

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

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

  6. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

  7. [LeetCode] Reverse String 翻转字符串

    Write a function that takes a string as input and returns the string reversed. Example: Given s = &q ...

  8. [LeetCode] Magical String 神奇字符串

    A magical string S consists of only '1' and '2' and obeys the following rules: The string S is magic ...

  9. [LeetCode] Reverse String II 翻转字符串之二

    Given a string and an integer k, you need to reverse the first k characters for every 2k characters ...

随机推荐

  1. linux安装jdk(以centos安装jdk1.7为例)

    1准备工作: 1 虚拟机一台vmware12,安装64位centos 2 oracle官网下载jdk1.7-linux-x64.rpm 3 winscp将jdk传送到linux上面 2开始安装: 1 ...

  2. break continue 区别 以及实例

    不论是MATLAB.c/c++.c#还是其他类型的编程语言,我们总是避免不了和for循环以及switch语句打交道,而对循环进行优化的时候,又总是避免不了用到break以及continue来控制循环, ...

  3. VS2012 调试时 局部变量显示不全的问题解决

    在工程上右键,打开属性页,配置属性——C/C++——优化,将优化改为“已禁用/Od“

  4. EXCEL里面的数字显示为文本 不用科学计数法显示

    1. 在输入这一串数字前加撇号“'”(英文状态下的单引号)即可.2. 先将这一列设置为“文本”格式,然后直接输入这一串数字即可.   已经输入好了数字,那估计你这些数字的后三位都已经全变成“0”了,用 ...

  5. JavaScript控制类名(className属性)

    语法:object.className =classname   (注意大小写) 作用:获取元素的class属性,为网页内的某个元素指定一个CSS样式来更改该元素的外观 示例: <!DOCTYP ...

  6. c#缓存介绍(转)

    缓存主要是为了提高数据的读取速度.因为服务器和应用客户端之间存在着流量的瓶颈,所以读取大容量数据时,使用缓存来直接为客户端服务,可以减少客户端与服务器端的数据交互,从而大大提高程序的性能. 本章从缓存 ...

  7. 批处理——putCMAC小版本

    @ECHO off del telcc.vbs del telcc.bat del ftp1.txt del ftp1.bat @echo off setlocal enabledelayedexpa ...

  8. Linux Shell shortcut

    Ctrl+a跳到第一个字符前Ctrl+x同上但再按一次会从新回到原位置 Details see below: Linux shell shortcut

  9. 驱动实现led,pwm和中断基础知识

    2015.4.8星期三 晴天 今天老师讲的内容是内核编写led和pwm驱动,实现花样灯和放歌的功能.理解应用和驱动的对接,最后自己实现了在放歌的时候根据歌曲的节奏亮灭一个小灯,应为两个独立的驱动都已经 ...

  10. vld使用

    1.下载VLD官方版本 2.安装 3.在vs里面的属性里->c/c++->常规->副含附加目录  C:\Program Files (x86)\Visual Leak Detecto ...