-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary-tree-right-side-view.py
More file actions
executable file
·55 lines (49 loc) · 1.45 KB
/
binary-tree-right-side-view.py
File metadata and controls
executable file
·55 lines (49 loc) · 1.45 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
# -*- coding: utf-8 -*-
"""
Created on Sat Jul 18 02:09:12 2020
@author: johnoyegbite
"""
# SOLVED!
"""
Problem:
Given a binary tree, imagine yourself standing on the right side of it,
return the values of the nodes you can see ordered from top to bottom.
Example:
Input: [1,2,3,null,5,null,4]
Output: [1, 3, 4]
Explanation:
1 <---
/ \
2 3 <---
\ \
5 4 <---
"""
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def rightSideView(self, root):
"""
type root: TreeNode
rtype: List[int]
"""
# Traverse level by level and append the last element in the level
# since the level would be appended from left to right and we need
# the right value.
if not root:
return []
levelNodes = [root]
sideView = []
while levelNodes:
sideView.append(levelNodes[-1].val)
newLevelNodes = []
for parent in levelNodes:
if parent.left:
newLevelNodes.append(parent.left)
if parent.right:
newLevelNodes.append(parent.right)
levelNodes = newLevelNodes
return sideView