Leetcode Tencent 50 Task14 236. Lowest Common Ancestor of a Binary Tree

描述

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

百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”

例如,给定如下二叉树: root = [3,5,1,6,2,0,8,null,null,7,4]

示例 1:

1
2
3
4
输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
输出: 3
解释: 节点 5 和节点 1 的最近公共祖先是节点 3

示例 2:

1
2
3
4
输入: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
输出: 5
解释: 节点 5 和节点 4 的最近公共祖先是节点 5。因为根据定义最近公共祖先节点可以为节点本身。

说明:

  • 所有节点的值都是唯一的。
  • p、q 为不同节点且均存在于给定的二叉搜索树中。

来源:力扣(LeetCode)
链接:236. Lowest Common Ancestor of a Binary Tree

解题思路

这道题在上一道题的基础上少了一个搜索的条件,那么原来二叉搜索树的节点值左<根<右的性质就不能用了,但是递归的思路还是可行的:

  • 边界条件:
    • 如果root为空,则返回NULL
    • 如果root等于其中某个节点,则返回root
  • 分而治之
    • left = lowestCommonAncestor(root->left, p, q)
    • right = lowestCommonAncestor(root->right, p, q)
  • 结果返回
    • 如果leftright都不为空时,说明pq一个在root的左子树,一个在root的右子树,返回root
    • 如果只有left有返回值,说明left的返回值是最小公共祖先
    • 如果只有right有返回值,说明right的返回值是最小公共祖先

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
if(!root) return NULL;
else if(root == q || root == p) return root;

//有可能返回p,q,NULL
struct TreeNode* left = lowestCommonAncestor(root->left, p, q);
struct TreeNode* right = lowestCommonAncestor(root->right, p, q);

if(left && right) return root;
return left ? left:right;
}