File tree Expand file tree Collapse file tree 1 file changed +20
-0
lines changed
Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Original file line number Diff line number Diff line change 1+ package Chap7 ;
2+
3+ public class LowestCommonAncestor {
4+ private class TreeNode {
5+ int val ;
6+ TreeNode left ;
7+ TreeNode right ;
8+ TreeNode (int x ) { val = x ; }
9+ }
10+
11+ public TreeNode lowestCommonAncestor (TreeNode root , TreeNode p , TreeNode q ) {
12+ if (root == null ) return null ; // 如果树为空,直接返回null
13+ if (root == p || root == q ) return root ; // 如果 p和q中有等于 root的,那么它们的最近公共祖先即为root(一个节点也可以是它自己的祖先)
14+ TreeNode left = lowestCommonAncestor (root .left , p , q ); // 递归遍历左子树,只要在左子树中找到了p或q,则先找到谁就返回谁
15+ TreeNode right = lowestCommonAncestor (root .right , p , q ); // 递归遍历右子树,只要在右子树中找到了p或q,则先找到谁就返回谁
16+ if (left == null ) return right ; // 如果在左子树中 p和 q都找不到,则 p和 q一定都在右子树中,右子树中先遍历到的那个就是最近公共祖先(一个节点也可以是它自己的祖先)
17+ else if (right == null ) return left ; // 否则,如果 left不为空,在左子树中有找到节点(p或q),这时候要再判断一下右子树中的情况,如果在右子树中,p和q都找不到,则 p和q一定都在左子树中,左子树中先遍历到的那个就是最近公共祖先(一个节点也可以是它自己的祖先)
18+ else return root ; //否则,当 left和 right均不为空时,说明 p、q节点分别在 root异侧, 最近公共祖先即为 root
19+ }
20+ }
You can’t perform that action at this time.
0 commit comments