Java 手撕二叉树,用递归,栈,bfs,dfs 遍历二叉树

1、首先创建一个节点类,生成相应的方法

public class TreeNode {
	TreeNode left;
	TreeNode right;
	int value;
	public TreeNode(int value) {
		this.value = value;
		this.left = null;
		this.right = null;
	}
	public TreeNode getLeft() {
		return left;
	}
	public void setLeft(TreeNode left) {
		this.left = left;
	}
	public TreeNode getRight() {
		return right;
	}
	public void setRight(TreeNode right) {
		this.right = right;
	}
	public int getValue() {
		return value;
	}
	public void setValue(int value) {
		this.value = value;
	}
	
	public String toString(){
		return (this.value+"");
	}
}

2、创建二叉树

    //创建二叉树
	public static void create(int[] dates,List list) {
		//将数组中的东西变为结点
		for(int i=0;i

3、bfs遍历

    //广度优先//层序遍历(用队列linkedlist)
	public static void bfs(TreeNode root) {
		if(root==null) return;
		//队列
		LinkedList qeque=new LinkedList<>();
		qeque.offer(root);//把根放进去 
		while(qeque.size()>0) {
			root=qeque.peek();
			System.out.print(root+" ");
			qeque.poll();
			if(root.getLeft()!=null) {
				qeque.offer(root.getLeft());
			}
			if(root.getRight()!=null) {
				qeque.offer(root.getRight());
			}
		}	
	}

4、dfs遍历

    //深度优先 用栈写,也是Linkedlist
   public static void dfs(TreeNode root) {
		if(root==null) return;
		Deque stack=new LinkedList<>();
		stack.offer(root);//offer和add都可以
		while(stack.size()>0) {
			TreeNode out=stack.pop();//弹出栈顶元素  
			System.out.print(out+" ");
			if(out.getRight()!=null) {
				stack.push(out.getRight());
			}
			if(out.getLeft()!=null) {
				stack.push(out.getLeft());
			}
		}
	}
	

 5、用栈实现前序遍历(根左右)

    //用栈实现前序遍历(根左右)
	public static void stack(TreeNode n) {
		Deque mystack=new LinkedList<>();
		
		TreeNode node=n;
		while(node!=null || !mystack.isEmpty()) {
			while(node!=null) {
				System.out.print(node.getValue()+"  ");
				mystack.push(node);//放入栈中
				node=node.getLeft();
			}
			if(!mystack.isEmpty()) {
				node=mystack.pop();//取出栈顶元素
				node=node.getRight();
			}
		}
	}

6、用栈实现中序遍历(左根右)

    //用栈实现中序遍历(左根右)
	public static void stack2(TreeNode n) {
		Deque mystack=new LinkedList<>();
		
		TreeNode node=n;
		while(node!=null || !mystack.isEmpty()) {
			while(node!=null) {
				mystack.push(node);//放入栈中
				node=node.getLeft();
			}
			if(!mystack.isEmpty()) {
				node=mystack.pop();//取出栈顶元素
				System.out.print(node.getValue()+"  ");
				node=node.getRight();
			}
		}
	}

7、用栈实现后序遍历(左右根)

    //用栈实现后序遍历(左右根)
	public static void stack3(TreeNode n) {
		Deque s=new LinkedList<>();
		TreeNode node=n; 
		TreeNode pre=n;
		while(node!=null||!s.isEmpty()) {
			while(node!=null) {
				s.push(node);
				node=node.getLeft();
			}
			if(!s.isEmpty()) {
				TreeNode temp=s.peek().getRight();
			
				if(temp==null|| temp==pre) {//此处做判断
					node=s.pop();	
					System.out.print(node.getValue()+"  ");
					pre=node;
					node=null;
				}else {
					node=temp;
				}
			}
		}
	}

 8、递归遍历

    //递归先序遍历
	public static void front(TreeNode n) {
		if(n==null) 
			return;
		System.out.print(n.getValue()+"  ");
		front(n.getLeft());
		front(n.getRight());	
	}
	
	//递归中序遍历
	public static void middle(TreeNode n) {
		 if (n == null)
	          return; 
		 middle(n.getLeft());
	     System.out.print(n.getValue()+" ");
	     middle(n.getRight());	
	}
	
	//递归后序遍历
	public static void behand(TreeNode n) {
		 if (n == null)
	          return; 
		 behand(n.getLeft());
		 behand(n.getRight());	
	     System.out.print(n.getValue()+" ");
	}

9、主方法进行测试

public static void main(String[] args) {
		int[] arr= {3,5,7,1,9,2};
		List list=new ArrayList<>();
		TestTree.create(arr,list);
		dfs(list.get(0));//采用深度优先输出
	}

 10,结果展示

3 5 1 9 7 2 

你可能感兴趣的:(深度优先,java,宽度优先)