文章

37

粉丝

68

获赞

6

访问

6.7k

头像
二叉树的最近公共祖先
综合
发布于2024年4月6日 10:03
阅读数 177

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null || root == p || root == q) return root;
        TreeNode left = lowestCommonAncestor(root.left, p, q);
        TreeNode right = lowestCommonAncestor(root.right, p, q);
        if(left == null) return right;
        if(right == null) return left;
        return root;
    }
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发