-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathheight-of-special-binary-tree.py
49 lines (44 loc) · 1.14 KB
/
height-of-special-binary-tree.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
# Time: O(n)
# Space: O(h)
# Definition for a binary tree node.
class TreeNode(object):
def __init__(self, val=0, left=None, right=None):
pass
# dfs
class Solution(object):
def heightOfTree(self, root):
"""
:type root: Optional[TreeNode]
:rtype: int
"""
result = -1
stk = [(root, 0)]
while stk:
u, d = stk.pop()
result = max(result, d)
if u.right and u.right.left != u:
stk.append((u.right, d+1))
if u.left and u.left.right != u:
stk.append((u.left, d+1))
return result
# Time: O(n)
# Space: O(w)
# bfs
class Solution2(object):
def heightOfTree(self, root):
"""
:type root: Optional[TreeNode]
:rtype: int
"""
result = -1
q = [root]
while q:
new_q = []
for u in q:
if u.left and u.left.right != u:
new_q.append(u.left)
if u.right and u.right.left != u:
new_q.append(u.right)
q = new_q
result += 1
return result