算法培训(二)---二叉树的遍历

定义节点:

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

KlkQaQ.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值