二叉树的遍历与搜索

记得深度优先用的是栈,广度优先用的是队列

深度优先包括我们之前说的前序中序后续遍历方法

广度优先就是层次遍历


package toyprogram;

import java.util.Queue;
import java.util.Stack;
import java.util.concurrent.ArrayBlockingQueue;

class TreeNode {
    Object val = null;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;
    }
    public TreeNode(TreeNode l,TreeNode r,Object o) {
		left=l;
		right=r;
		val=o;
	}

}

public class Solution {
	public static void main(String[] args) {
		TreeNode treeNode=getTree();
		depthFirstSearch(treeNode);
		System.out.println("*****");
		breadthFirstSearch(treeNode);
	}
	public static TreeNode getTree(){
		TreeNode G = new TreeNode(null, null, 'G');
		TreeNode H = new TreeNode(null, null, 'H');
		TreeNode F = new TreeNode(G, H, 'F');
		TreeNode D = new TreeNode(null, F, 'D');
		TreeNode E = new TreeNode(null, null, 'E');
		TreeNode B = new TreeNode(D, E, 'B');
		TreeNode C = new TreeNode(null, null, 'C');
		TreeNode A = new TreeNode(B, C, 'A');
		return A;
	}
	
	public static void breadthFirstSearch(TreeNode root){
		Queue<TreeNode> queue=new ArrayBlockingQueue<TreeNode>(10);
		
        if (root!=null) {
			queue.add(root);
		}else {
			return;
		}
        
        while (!queue.isEmpty()) {
			TreeNode t=queue.poll();
			System.out.print(t.val);
			if (t.left!=null) 
				queue.add(t.left);
			
			if(t.right!=null)
				queue.add(t.right);
			
		}
	}
	
    public static void depthFirstSearch(TreeNode root) {
    	Stack<TreeNode> s=new Stack<>();
        if (root!=null) {
			s.push(root);
		}else {
			return;
		}
        
        while (!s.empty()) {
			TreeNode treeNode=s.pop();
			System.out.print(treeNode.val);
			if (treeNode.right!=null) 
				s.push(treeNode.right);
			
			if (treeNode.left!=null) 
				s.push(treeNode.left);
			
			
		}
    }
}

这两天面试,人家让写二叉树的查找,想了一下,代码如下:

	/**
	 * 递归解法
	 * 
	 * @param t
	 * @param target
	 * @return
	 */
	public static TreeNode findTreeWithRecursion(TreeNode t, int target) {

		if (t != null) {
			if (t.val == target) {
				return t;
			}
			TreeNode t1, t2;
			t1 = findTreeWithRecursion(t.left, target);
			if (t1 != null)
				return t1;

			// 如果把下面的代码 上移3行 效率就会降低很多
			// 大家能理解么?
			t2 = findTreeWithRecursion(t.right, target);
			if (t2 != null)
				return t2;

			return null;

		} else {
			return null;
		}

	}

	/**
	 * 非递归解法
	 * 
	 * @param t
	 * @param target
	 * @return
	 */
	public static TreeNode findTreeWithoutRecursion(TreeNode t, int target) {

		Stack<TreeNode> s = new Stack<>();
		if (t != null) {
			s.push(t);
		} else {
			return null;
		}

		while (!s.empty()) {
			TreeNode treeNode = s.pop();
			if (t.val == target) {
				return t;
			}
			if (treeNode.right != null) {
				s.push(treeNode.right);
			}
			if (treeNode.left != null) {
				s.push(treeNode.left);
			}

		}

		return null;
	}






  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值