列表

详情


1676. 二叉树的最近公共祖先 IV

给定一棵二叉树的根节点 root 和 TreeNode 类对象的数组(列表) nodes,返回 nodes 中所有节点的最近公共祖先(LCA)。数组(列表)中所有节点都存在于该二叉树中,且二叉树中所有节点的值都是互不相同的。

我们扩展二叉树的最近公共祖先节点在维基百科上的定义:“对于任意合理的 i 值, n 个节点 p1 、 p2、...、 pn 在二叉树 T 中的最近公共祖先节点是后代中包含所有节点 pi 的最深节点(我们允许一个节点是其自身的后代)”。一个节点 x 的后代节点是节点 x 到某一叶节点间的路径中的节点 y

 

示例 1:

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

示例 2:

输入: root = [3,5,1,6,2,0,8,null,null,7,4], nodes = [1]
输出: 1
解释: 单个节点的最近公共祖先是该节点本身。

示例 3:

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

示例 4:

输入: root = [3,5,1,6,2,0,8,null,null,7,4], nodes = [0,1,2,3,4,5,6,7,8]
输出: 3
解释: 树中所有节点的最近公共祖先是根节点。

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: TreeNode* lowestCommonAncestor(TreeNode* root, vector<TreeNode*> &nodes) { } };

java 解法, 执行用时: 0 ms, 内存消耗: 43.3 MB, 提交时间: 2023-10-16 21:54:21

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode[] nodes) {
        if (root==null){
            return null;
        }
        for (TreeNode node:nodes){
            if (node==root){
                return root;
            }
        }
        TreeNode left=lowestCommonAncestor(root.left,nodes);
        TreeNode right=lowestCommonAncestor(root.right,nodes);
        if (left!=null && right!=null){
            return root;
        }
        return left!=null?left:right;
    }
}

java 解法, 执行用时: 0 ms, 内存消耗: 43.3 MB, 提交时间: 2023-10-16 21:54:01

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode[] nodes) {
        if (root == null) {
            return null;
        }
        
        if (isIn(nodes, root)) {
            return root;
        } else {
            TreeNode res = null;
            TreeNode left =  lowestCommonAncestor(root.left, nodes);
            TreeNode right = lowestCommonAncestor(root.right, nodes);
            if (left != null && right != null) {
                return root;
            }
            res =  (left == null) ? right : left;
            return res;
        }
        
    }
    public boolean isIn(TreeNode[] nodes, TreeNode root) {
        for (int i = 0; i < nodes.length; i++) {
            if (nodes[i] == root) {
                return true;
            }
        }
        return false;
    }
}

python3 解法, 执行用时: 124 ms, 内存消耗: 21.3 MB, 提交时间: 2023-10-16 21:53:37

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def lowestCommonAncestor(self, root: 'TreeNode', nodes: 'List[TreeNode]') -> 'TreeNode':
        # dfs_LRN 后序遍历求LCA
        if root==None or root in nodes:
            return root
        L = self.lowestCommonAncestor(root.left, nodes)
        R = self.lowestCommonAncestor(root.right, nodes)
        if L and R:
            return root
        elif L and not R:
            return L
        elif not L and R:
            return R
        else:
            return None

cpp 解法, 执行用时: 56 ms, 内存消耗: 41.4 MB, 提交时间: 2023-10-16 21:53:23

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, vector<TreeNode*>& nodes) {
    	if (!root) return root;
    	if (count(nodes.begin(), nodes.end(), root))
    		return root;
    	TreeNode* left = lowestCommonAncestor(root->left, nodes);
    	TreeNode* right = lowestCommonAncestor(root->right, nodes);
    	if (left && right)
    		return root;
    	return left ? left : right;
    }
};

javascript 解法, 执行用时: 100 ms, 内存消耗: 57.1 MB, 提交时间: 2023-10-16 21:52:41

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @param {TreeNode[]} nodes
 * @return {TreeNode}
 */
var lowestCommonAncestor = function(root, nodes) {
    if(!root || nodes.includes(root)) return root

    let left = lowestCommonAncestor(root.left,nodes)
    let right = lowestCommonAncestor(root.right,nodes)

    if(left && right) return root
    return  left || right 
};

上一题