We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2d98ee1 commit 1232309Copy full SHA for 1232309
Binary Tree Right Side View/Solution.rb
@@ -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
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
29
+ right_side
30
+end
0 commit comments