【LeetCode每日一题】——236.二叉树的最近公共祖先

一【题目类别】

  • 深度优先搜索

二【题目难度】

  • 中等

三【题目编号】

  • 236.二叉树的最近公共祖先

四【题目描述】

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

五【题目示例】

  • 示例 1:

    • 在这里插入图片描述
    • 输入:root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
    • 输出:3
    • 解释:节点 5 和节点 1 的最近公共祖先是节点 3 。
  • 示例 2:
    在这里插入图片描述

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

    • 输入:root = [1,2], p = 1, q = 2
    • 输出:1

六【解题思路】

  • 这个题本来想利用数组根据下标去找公共祖先,但是这显然是不允许的,以为题目给的二叉树并不是完全二叉树
  • 那么我们就需要换一个思路
  • 我们首先遍历这棵树,判断是否能找到题目给定的节点(p=left,q=right),一共分四种情况
    • left和right都不为空:说明p和q异侧,root就是最近的公共祖先,返回root
    • left和right都为空:说明二叉树中不存在p和q,返回null
    • left不为空right为空:p和q同侧(left侧),最近公共祖先在left,返回left
    • right不为空left为空:p和q同侧(right侧),最近公共祖先在right,返回right

七【题目提示】

  • 树中节点数目在范围 [ 2 , 1 0 5 ] 内。 树中节点数目在范围 [2, 10^5] 内。 树中节点数目在范围[2,105]内。
  • − 1 0 9 < = N o d e . v a l < = 1 0 9 -10^9 <= Node.val <= 10^9 109<=Node.val<=109
  • 所有 N o d e . v a l 互不相同。 所有 Node.val 互不相同 。 所有Node.val互不相同。
  • p ! = q p != q p!=q
  • p 和 q 均存在于给定的二叉树中。 p 和 q 均存在于给定的二叉树中。 pq均存在于给定的二叉树中。

八【时间频度】

  • 时间复杂度: O ( n ) O(n) O(n),其中 n n n 是二叉树的节点个数
  • 空间复杂度: O ( n ) O(n) O(n),其中 n n n 是二叉树的节点个数

九【代码实现】

  1. Java语言版
package DFS;

public class p236_LowestCommonAncestorOfABinaryTree {

    int val;
    p236_LowestCommonAncestorOfABinaryTree left;
    p236_LowestCommonAncestorOfABinaryTree right;

    public p236_LowestCommonAncestorOfABinaryTree(int val) {
        this.val = val;
    }

    public p236_LowestCommonAncestorOfABinaryTree(int val, p236_LowestCommonAncestorOfABinaryTree left, p236_LowestCommonAncestorOfABinaryTree right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }

    public static void main(String[] args) {
        p236_LowestCommonAncestorOfABinaryTree root = new p236_LowestCommonAncestorOfABinaryTree(3);
        p236_LowestCommonAncestorOfABinaryTree left = new p236_LowestCommonAncestorOfABinaryTree(5);
        p236_LowestCommonAncestorOfABinaryTree right = new p236_LowestCommonAncestorOfABinaryTree(1);
        p236_LowestCommonAncestorOfABinaryTree left1 = new p236_LowestCommonAncestorOfABinaryTree(6);
        p236_LowestCommonAncestorOfABinaryTree left2 = new p236_LowestCommonAncestorOfABinaryTree(2);
        p236_LowestCommonAncestorOfABinaryTree right1 = new p236_LowestCommonAncestorOfABinaryTree(0);
        p236_LowestCommonAncestorOfABinaryTree right2 = new p236_LowestCommonAncestorOfABinaryTree(8);
        p236_LowestCommonAncestorOfABinaryTree left3 = new p236_LowestCommonAncestorOfABinaryTree(7);
        p236_LowestCommonAncestorOfABinaryTree left4 = new p236_LowestCommonAncestorOfABinaryTree(4);
        root.left = left;
        root.right = right;
        left.left = left1;
        left.right = left2;
        left2.left = left3;
        left2.right = left4;
        right.left = right1;
        right.right = right2;
        p236_LowestCommonAncestorOfABinaryTree ree = lowestCommonAncestor(root, left, right);
        System.out.println("ree = " + ree.val);
    }

    public static p236_LowestCommonAncestorOfABinaryTree lowestCommonAncestor(p236_LowestCommonAncestorOfABinaryTree root, p236_LowestCommonAncestorOfABinaryTree p, p236_LowestCommonAncestorOfABinaryTree q) {
        if (root == null) {
            return root;
        }
        if (root == p || root == q) {
            return root;
        }
        p236_LowestCommonAncestorOfABinaryTree left = lowestCommonAncestor(root.left, p, q);
        p236_LowestCommonAncestorOfABinaryTree right = lowestCommonAncestor(root.right, p, q);
        if (left != null && right != null) {
            return root;
        } else if (left != null) {
            return left;
        } else if (right != null) {
            return right;
        }
        return null;
    }

}
  1. C语言版
#include<stdio.h>

struct TreeNode {
	int val;
	struct TreeNode *left;
	struct TreeNode *right;
};

struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q)
{
	if (root == NULL)
	{
		return root;
	}
	if (root == p || root == q)
	{
		return root;
	}
	struct TreeNode* left = lowestCommonAncestor(root->left, p, q);
	struct TreeNode* right = lowestCommonAncestor(root->right, p, q);
	if (left != NULL && right != NULL)
	{
		return root;
	}
	else if (left != NULL)
	{
		return left;
	}
	else if (right != NULL)
	{
		return right;
	}
	return NULL;
}

/*主函数省略*/

十【提交结果】

  1. Java语言版
    在这里插入图片描述

  2. C语言版
    在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IronmanJay

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值