Skip to content

Commit 1232309

Browse files
committed
Right side view
1 parent 2d98ee1 commit 1232309

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode
3+
# attr_accessor :val, :left, :right
4+
# def initialize(val)
5+
# @val = val
6+
# @left, @right = nil, nil
7+
# end
8+
# end
9+
10+
# @param {TreeNode} root
11+
# @return {Integer[]}
12+
def right_side_view(root)
13+
right_side=[]
14+
return right_side if root.nil?
15+
q = []
16+
q.push(root)
17+
until q.empty?
18+
size = q.length
19+
1.upto(size) do |i|
20+
top = q.shift
21+
22+
right_side.push top.val if i==1 # first element is the right most element of that level
23+
24+
q.push(top.right) unless top.right.nil?
25+
q.push(top.left) unless top.left.nil?
26+
27+
end
28+
end
29+
right_side
30+
end

0 commit comments

Comments
 (0)