这是一篇小白测试
package com.my.test;
import java.util.ArrayList;
import java.util.List;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
class Solution{
public static void main(String[] args) {
Integer[] a = new Integer[]{1,null,2,3};
TreeNode root = new TreeNode();
root = creatBiTree(root,a,0);
System.out.println(inorderTraversal(root));
}
//中序遍历
public static List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
if (root == null)
return list;
if (root.left != null)
list.addAll(inorderTraversal(root.left));
list.add(root.val);
if (root.right != null)
list.addAll(inorderTraversal(root.right));
return list;
}
//创建二叉树
public static TreeNode creatBiTree(TreeNode root,Integer[] a,int i){
if (i < a.length){
if (a[i] == null){
root = null;
}else {
TreeNode lt = new TreeNode();
TreeNode rt = new TreeNode();
root.val = a[i];
root.left = creatBiTree(lt,a,++i);
root.right = creatBiTree(rt,a,++i);
}
}
return root;
}
}
标签:right,TreeNode,val,博客,发布,测试,null,root,left
From: https://www.cnblogs.com/humh/p/17295404.html