设计算法查找二叉树的两个结点最近公共祖先(LCA)

public static BinaryTreeNode<Integer> LCA(BinaryTreeNode<Integer> root,BinaryTreeNode<Integer> a, BinaryTreeNode<Integer> b){
		BinaryTreeNode<Integer> left,right;
		if(root==null)
			return root;
		if(root==a|| root==b)
			return root;
		left = LCA(root.getleft(),a,b);
		right = LCA(root.getright(),a,b);
		if(left!=null && right!=null)
			return root;
		else
			return(left!=null?left:right);
	}

发布了32 篇原创文章 · 获赞 1 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/lhf2112/article/details/70169896