定义节点:
public class TreeNode {
public TreeNode left;
public TreeNode right;
public int val;
public TreeNode(int val)
{
this.val=val;
}
}
建树,遍历:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
public class TreeUnit1 {
public static TreeNode CreatTreeByString(String tree){//传入字符串
tree.trim();//除去左右两端空格
Queue<TreeNode> queue;//队列初始化
queue = new LinkedList<TreeNode>();
String[] value = tree.split(",");
String item = value[0];
TreeNode root = new TreeNode(Integer.parseInt(item));//定义根节点
queue.offer(root);//根节点入队列
int index = 0;
while(!queue.isEmpty())//判断队列是否为空
{
TreeNode p = queue.poll();//出队列
if(index==value.length-1)break;//判断队列是否为空
item = value[++index];
if(!item.equals("null")){
TreeNode left = new TreeNode(Integer.parseInt(item)); //新建子节点
p.left = left;
queue.offer(left);
}
if(index==value.length-1)break;
item = value[++index];
if(!item.equals("null")){
TreeNode right = new TreeNode(Integer.parseInt(item));
p.right = right;
queue.offer(right);
}
}
return root;
}
public static void preorderRecursive(TreeNode root,List<Integer> ans)//前序遍历---递归
{
if(root == null)return;
ans.add(root.val);
preorderRecursive(root.left,ans);
preorderRecursive(root.right,ans);
}
public static void Iteration(TreeNode root,List<Integer> ans) //前序遍历---迭代
{
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
while(!stack.isEmpty())
{
TreeNode p =stack.pop();
ans.add(p.val);
if(p.right!=null){
stack.push(p.right);
}
if(p.left!=null){
stack.push(p.left);
}
}
}
public static void SequentialTraversal(TreeNode root,List<Integer> ans)//中序遍历---递归
{
if(root==null)
return;
SequentialTraversal(root.left,ans);
ans.add(root.val);
SequentialTraversal(root.right,ans);
}
public static void IterationTraversal(TreeNode root,List<Integer> ans)//中序遍历---迭代
{
Stack<TreeNode> stack = new Stack<TreeNode>();
while(!stack.isEmpty()||root!=null){
while(root!=null){
stack.push(root);
root=root.left;
}
root = stack.pop();
ans.add(root.val);
root=root.right;
}
}
public static void DFSlevelItraveral(TreeNode root,int level,List<List<Integer>> list1)//层序遍历---DFS
{
if(root==null)
return;
if(level + 1 > list1.size()) {
list1.add(new LinkedList<Integer>());
}
list1.get(level).add(root.val);
DFSlevelItraveral(root.left,level+1,list1);
DFSlevelItraveral(root.right,level+1,list1);
}
public static void BFSleveltraveral(TreeNode root,List<Integer> ans)//层序遍历---BFS
{
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
while(!queue.isEmpty()){
TreeNode p = queue.poll();
ans.add(p.val);
if(p.left!=null)
{
queue.offer(p.left);
}
if(p.right!=null)
{
queue.offer(p.right);
}
}
}
public static void main(String[] args) {
String s = "1,2,3,null,4,5,null,6,7,null,null,8,9,10";
TreeNode root = CreatTreeByString(s);
int testTime =500000;
long startTime = System.currentTimeMillis();
for(int i=0;i < testTime;i++) {
List<Integer> ans = new ArrayList<Integer>();
List<List<Integer>> list1 = new LinkedList();
// preorderRecursive(root,ans);
// Iteration(root,ans);
// SequentialTraversal(root,ans);
// IterationTraversal(root,ans);
int level = 0;
//BFSleveltraveral(root,ans);
DFSlevelItraveral(root,level,list1);
}
long endTime = System.currentTimeMillis();
System.out.println("Time:"+(endTime-startTime));
}
}