Skip to content

Latest commit

 

History

History
30 lines (28 loc) · 814 Bytes

865.md

File metadata and controls

30 lines (28 loc) · 814 Bytes

865. Smallest Subtree with all the Deepest Nodes

Solution 1 (time O(n), space O(n))

# DFS
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def subtreeWithAllDeepest(self, root):
        """
        :type root: TreeNode
        :rtype: TreeNode
        """
        def dfs(root):
            if root is None:
                return None, 0
            l, d1 = dfs(root.left)
            r, d2 = dfs(root.right)
            if d1 > d2:
                return l, d1 + 1
            if d1 < d2:
                return r, d2 + 1
            return root, d1 + 1
        return dfs(root)[0]