-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathsmallest-subtree-with-all-the-deepest-nodes.py
55 lines (50 loc) · 1.59 KB
/
smallest-subtree-with-all-the-deepest-nodes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# V0
# V1
# https://blog.csdn.net/fuxuemingzhu/article/details/82432130
# https://www.cnblogs.com/grandyang/p/10703653.html
# IDEA :
# CASE 1) : depth(left) == depth(right) -> return root
# CASE 2) : depth(left) > depth(right) -> return left , depth(left) < depth(right) -> return right
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def subtreeWithAllDeepest(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
return self.depth(root)[1]
def depth(self, root):
if not root: return 0, None
l, r = self.depth(root.left), self.depth(root.right)
if l[0] > r[0]:
return l[0] + 1, l[1]
elif l[0] < r[0]:
return r[0] + 1, r[1]
else:
return l[0] + 1, root
# V2
# Time: O(n)
# Space: O(h)
import collections
class Solution(object):
def subtreeWithAllDeepest(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
Result = collections.namedtuple("Result", ("node", "depth"))
def dfs(node):
if not node:
return Result(None, 0)
left, right = dfs(node.left), dfs(node.right)
if left.depth > right.depth:
return Result(left.node, left.depth+1)
if left.depth < right.depth:
return Result(right.node, right.depth+1)
return Result(node, left.depth+1)
return dfs(root).node