Medium

题目描述

给你一个有根节点 root 的二叉树,返回它 最深的叶节点的最近公共祖先

回想一下:

  • 叶节点 是二叉树中没有子节点的节点
  • 树的根节点的 深度0,如果某一节点的深度为 d,那它的子节点的深度就是 d+1
  • 如果我们假定 A 是一组节点 S最近公共祖先S 中的每个节点都在以 A 为根节点的子树中,且 A 的深度达到此条件下可能的最大值。

示例 1:

输入:root = [3,5,1,6,2,0,8,null,null,7,4]
输出:[2,7,4]
解释:我们返回值为 2 的节点,在图中用黄色标记。
在图中用蓝色标记的是树的最深的节点。
注意,节点 6、0 和 8 也是叶节点,但是它们的深度是 2 ,而节点 7 和 4 的深度是 3 。

示例 2:

输入:root = [1]
输出:[1]
解释:根节点是树中最深的节点,它是它本身的最近公共祖先。

示例 3:

输入:root = [0,1,3,null,2]
输出:[2]
解释:树中最深的叶节点是 2 ,最近公共祖先是它本身。

提示:

  • 树中节点的数量在 [1, 1000] 范围内。
  • 0 <= Node.val <= 1000
  • 每个节点的值都是 独一无二 的。

**注意:**本题与力扣 865 重复:https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/

解题思路

这道题要求找到最深叶节点的最近公共祖先。我们可以采用后序遍历的思路来解决。

核心思想: 对于任意节点,如果它的左右子树中都包含最深的叶节点,那么它就是最深叶节点的最近公共祖先。如果只有一边包含最深叶节点,那么答案就在包含最深叶节点的那一边。

算法步骤:

  1. 使用递归函数返回两个值:当前子树的最大深度和包含最深叶节点的子树根节点
  2. 对于每个节点,递归计算左右子树的深度和对应的答案节点
  3. 如果左右子树深度相等且都是最深的,当前节点就是答案
  4. 如果左子树更深,答案在左子树中
  5. 如果右子树更深,答案在右子树中

时间复杂度: O(n),每个节点访问一次 空间复杂度: O(h),其中 h 是树的高度,递归栈空间

这种一次遍历的解法比先找最深深度再找LCA的两次遍历方法更加高效。

代码实现

class Solution {
public:
    TreeNode* lcaDeepestLeaves(TreeNode* root) {
        return dfs(root).second;
    }
    
private:
    pair<int, TreeNode*> dfs(TreeNode* node) {
        if (!node) return {0, nullptr};
        
        auto left = dfs(node->left);
        auto right = dfs(node->right);
        
        int leftDepth = left.first;
        int rightDepth = right.first;
        
        if (leftDepth > rightDepth) {
            return {leftDepth + 1, left.second};
        } else if (leftDepth < rightDepth) {
            return {rightDepth + 1, right.second};
        } else {
            return {leftDepth + 1, node};
        }
    }
};
class Solution:
    def lcaDeepestLeaves(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
        def dfs(node):
            if not node:
                return 0, None
            
            left_depth, left_lca = dfs(node.left)
            right_depth, right_lca = dfs(node.right)
            
            if left_depth > right_depth:
                return left_depth + 1, left_lca
            elif left_depth < right_depth:
                return right_depth + 1, right_lca
            else:
                return left_depth + 1, node
        
        return dfs(root)[1]
public class Solution {
    public TreeNode LcaDeepestLeaves(TreeNode root) {
        return Dfs(root).Item2;
    }
    
    private (int, TreeNode) Dfs(TreeNode node) {
        if (node == null) return (0, null);
        
        var left = Dfs(node.left);
        var right = Dfs(node.right);
        
        int leftDepth = left.Item1;
        int rightDepth = right.Item1;
        
        if (leftDepth > rightDepth) {
            return (leftDepth + 1, left.Item2);
        } else if (leftDepth < rightDepth) {
            return (rightDepth + 1, right.Item2);
        } else {
            return (leftDepth + 1, node);
        }
    }
}
var lcaDeepestLeaves = function(root) {
    function dfs(node) {
        if (!node) return [0, null];
        
        const [leftDepth, leftLca] = dfs(node.left);
        const [rightDepth, rightLca] = dfs(node.right);
        
        if (leftDepth > rightDepth) {
            return [leftDepth + 1, leftLca];
        } else if (leftDepth < rightDepth) {
            return [rightDepth + 1, rightLca];
        } else {
            return [leftDepth + 1, node];
        }
    }
    
    return dfs(root)[1];
};

复杂度分析

复杂度类型说明
时间复杂度O(n)需要遍历树中的每个节点一次
空间复杂度O(h)递归调用栈的深度,h为树的高度,最坏情况下为O(n)

相关题目