forked from tcandzq/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreeRightSideView.py
More file actions
86 lines (69 loc) · 2.27 KB
/
BinaryTreeRightSideView.py
File metadata and controls
86 lines (69 loc) · 2.27 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# -*- coding: utf-8 -*-
# @File : BinaryTreeRightSideView.py
# @Date : 2020-02-29
# @Author : tc
"""
题号 199. 二叉树的右视图
给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
示例:
输入: [1,2,3,null,5,null,4]
输出: [1, 3, 4]
解释:
1 <---
/ \
2 3 <---
\ \
5 4 <---
参考:https://leetcode-cn.com/problems/binary-tree-right-side-view/solution/er-cha-shu-de-you-shi-tu-by-leetcode/
"""
from typing import List
from collections import deque
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def rightSideView(self, root: TreeNode) -> List[int]:
if not root:
return []
res,queue = [],[root]
while queue:
temp = 0
n = len(queue)
for i in range(n):
node = queue.pop(0)
temp = node.val
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
res.append(temp)
return res
def rightSideView2(self, root):
rightmost_value_at_depth = dict() # depth -> node.val
max_depth = -1
queue = deque([(root, 0)])
while queue:
node, depth = queue.popleft()
if node is not None:
# maintain knowledge of the number of levels in the tree.
max_depth = max(max_depth, depth)
# overwrite rightmost value at current depth. the correct value
# will never be overwritten, as it is always visited last.
rightmost_value_at_depth[depth] = node.val
queue.append((node.left, depth + 1))
queue.append((node.right, depth + 1))
return [rightmost_value_at_depth[depth] for depth in range(max_depth + 1)]
if __name__ == '__main__':
root = TreeNode(3)
node1 = TreeNode(9)
node2 = TreeNode(20)
node3 = TreeNode(15)
node4 = TreeNode(7)
root.left = node1
root.right = node2
node2.left = node3
node2.right = node4
solution = Solution()
print(solution.rightSideView(root))