二叉树遍历

/**
	 * 前序  深度优先
	 * LANG
	 * @param Node
	 */
	public static void preOrder1(TreeNode Node) {
		Stack<TreeNode> stack = new Stack<>();
		while (Node != null || !stack.empty()) {
			while (Node != null) {
				System.out.print(Node.val + "   ");
				stack.push(Node);
				Node = Node.left;
			}
			if (!stack.empty()) {
				Node = stack.pop();
				Node = Node.right;
			}
		}
	}
	/**
	 * 中序遍历
	 * LANG
	 * @param Node
	 */
	public static void midOrder1(TreeNode Node) {
		Stack<TreeNode> stack = new Stack<>();
		while (Node != null || !stack.empty()) {
			while (Node != null) {
				stack.push(Node);
				Node = Node.left;
			}
			if (!stack.empty()) {
				Node = stack.pop();
				System.out.print(Node.val + "   ");
				Node = Node.right;
			}
		}
	}
	
	/**
     * 后序遍历
     * 非递归
     */
	public static void posOrder1(TreeNode Node) {
		Stack<TreeNode> stack1 = new Stack<>();
		Stack<Integer> stack2 = new Stack<>();
		int i = 1;
		while (Node != null || !stack1.empty()) {
			while (Node != null) {
				stack1.push(Node);
				stack2.push(0);
				Node = Node.left;
			}

			while (!stack1.empty() && stack2.peek() == i) {
				stack2.pop();
				TreeNode Node1 = stack1.pop();
				System.out.print(Node1.val + "   ");
			}

			if (!stack1.empty()) {
				stack2.pop();
				stack2.push(1);
				Node = stack1.peek();
				Node = Node.right;
			}
		}
	}
    
	/*
     * 层序遍历 广度优先
     * 非递归
     */
	public static void levelOrder1(TreeNode Node) {
		if (Node == null) {
			return;
		}

		Queue<TreeNode> queue = new LinkedList<>();
		queue.add(Node);

		while (queue.size() != 0) {
			Node = queue.poll();

			System.out.print(Node.val + "  ");

			if (Node.left != null) {
				queue.offer(Node.left);
			}
			if (Node.right != null) {
				queue.offer(Node.right);
			}
		}
	}
    

https://www.cnblogs.com/liuyang0/p/6271331.html

给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root==null) {
			return root;
		}
		while(root!=null){
			List<TreeNode> pList=preOrder1(root, p);
			List<TreeNode> qList=preOrder1(root, q);
			int length=pList.size()<qList.size()?pList.size():qList.size();
			
			for (int i = 0; i < length; i++) {
				if (pList.get(i).val!=qList.get(i).val) {
					return pList.get(i-1);
				}
			}
		}
		
		return root;
    }
    public List<TreeNode> preOrder1(TreeNode Node,TreeNode input) {
		Stack<TreeNode> stack = new Stack<>();
		List<TreeNode> list=new ArrayList<>();
		while (Node != null || !stack.empty()) {
			while (Node != null) {
//				System.out.print(Node.val + "   ");
				list.add(Node);
				if (Node.val==input.val) {
					break;
				}
				stack.push(Node);
				Node = Node.left;
			}
			if (!stack.empty()) {
				Node = stack.pop();
				Node = Node.right;
			}
		}
		return list;
	}
}

上述方法会超过内存

解题思路:递归搜索左右子树,如果左子树和右子树都不为空,说明最近父节点一定在根节点。

反之,如果左子树为空,说明两个节点一定在右子树;同理如果右子树为空,说明两个节点一定在左子树。

public static TreeNode lowestCommonAncestor3(TreeNode root, TreeNode p, TreeNode q) {
		if (root == null || root == p || root == q)
			return root;
		TreeNode left = lowestCommonAncestor3(root.left, p, q);
		TreeNode right = lowestCommonAncestor3(root.right, p, q);
		if (left == null) {
			return right;
		}else {
			if (right==null) {
				return left;
			}else {
				return root;
			}
		}
//		return left == null ? right : right == null ? left : root;
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值