-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBinaryTreeRightSideView_v1.py
More file actions
52 lines (39 loc) · 1019 Bytes
/
BinaryTreeRightSideView_v1.py
File metadata and controls
52 lines (39 loc) · 1019 Bytes
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
#!/usr/bin/env python
# encoding: utf-8
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def __repr__(self):
return '<{}>'.format(self.val)
class Solution:
# @param {TreeNode} root
# @return {integer[]}
def rightSideView(self, root):
if not root:
return []
result = []
self.rightView(root, result, 0)
return result
def rightView(self, node, result, depth):
if not node:
return
if len(result) == depth:
result.append(node.val)
self.rightView(node.right, result, depth+1)
self.rightView(node.left, result, depth+1)
if __name__ == '__main__':
s = Solution()
n1 = TreeNode(1)
n2 = TreeNode(2)
n3 = TreeNode(3)
n4 = TreeNode(4)
n5 = TreeNode(5)
r = n1
n1.left = n2
n1.right = n3
n2.right = n5
n3.right = n4
print s.rightSideView(r)