1123. Lowest Common Ancestor of Deepest Leaves

link to problem

Description:

Given a rooted binary tree, return the lowest common ancestor of its deepest leaves.

Recall that:

  • The node of a binary tree is a leaf if and only if it has no children
  • The depth of the root of the tree is 0, and if the depth of a node is d, the depth of each of its children is d+1.
  • The lowest common ancestor of a set S of nodes is the node A with the largest depth such that every node in S is in the subtree with root A.

Solution:

class Solution:
    def lcaDeepestLeaves(self, root: TreeNode) -> TreeNode:

        def helper(node):
            if not node:
                return [node, 0]
            if not node.left and not node.right:
                return [node, 0]

            if not node.right:
                left_node, left_dep = helper(node.left)
                return [left_node, left_dep + 1]

            if not node.left:
                right_node, right_dep = helper(node.right)
                return [right_node, right_dep + 1]

            left_node, left_dep = helper(node.left)
            right_node, right_dep = helper(node.right)
            if left_dep > right_dep:
                return [left_node, left_dep + 1]
            elif left_dep < right_dep:
                return [right_node, right_dep + 1]
            else:
                return [node, left_dep + 1]

        return helper(root)[0]

Notes:

DFS

recursion

原文地址:https://www.cnblogs.com/beatets/p/12170587.html

时间: 2024-11-11 07:25:56

1123. Lowest Common Ancestor of Deepest Leaves的相关文章

[LeetCode] 1123. Lowest Common Ancestor of Deepest Leaves 最深叶结点的最小公共父节点

Given a rooted binary tree, return the lowest common ancestor of its deepest leaves. Recall that: The node of a binary tree is a?leaf?if and only if it has no children The?depth?of the root of the tree is 0, and if the depth of a node is?d, the depth

LeetCode 1123. Lowest Common Ancestor of Deepest Leaves

原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/ 题目: Given a rooted binary tree, return the lowest common ancestor of its deepest leaves. Recall that: The node of a binary tree is a leaf if and only if it has no childre

A1143. Lowest Common Ancestor

The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants. A binary search tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a nod

PAT 1143 Lowest Common Ancestor[难][BST性质]

1143 Lowest Common Ancestor(30 分) The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants. A binary search tree (BST) is recursively defined as a binary tree which has the following pro

PAT 1143 Lowest Common Ancestor

The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants. A binary search tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a nod

PAT 甲级 1143 Lowest Common Ancestor

https://pintia.cn/problem-sets/994805342720868352/problems/994805343727501312 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants. A binary search tree (BST) is recursively defined

1143 Lowest Common Ancestor

The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants. A binary search tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a nod

235. Lowest Common Ancestor of a Binary Search Tree

1. 问题描述 Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T th

Lowest Common Ancestor of a Binary Search Tree

1. Title 235. Lowest Common Ancestor of a Binary Search Tree 2. Http address https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ 3. The question Given a binary search tree (BST), find the lowest common ancestor (LCA) of two