Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 31dd141

Browse files
leetcode level order traversal
1 parent a59e4cb commit 31dd141

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ solution of many challenges of [Leetcode](https://leetcode.com/), [Exercism](htt
209209
8. [Balanced Binary tree](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/balanced_binary_tree.rb)
210210
9. [Symmetric Tree](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/symmetric_tree.rb)
211211
10. [Subtree of Another Tree](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/subtree_of_another_tree.rb)
212-
212+
11.[Binary Tree Level order Traversal](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/binary_tree_level_order_traversal.rb)
213213

214214
<a name="bst"/>
215215

@@ -308,6 +308,7 @@ solution of many challenges of [Leetcode](https://leetcode.com/), [Exercism](htt
308308

309309
## [LeetCode](https://leetcode.com/problems)
310310
1. [Insert Delete GetRandom in O(1)](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/insert_delete_get_random_o1.rb)
311+
2. [Binary Tree Level order Traversal](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/binary_tree_level_order_traversal.rb)
311312

312313
<a name="grind-75"/>
313314

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
=begin
2+
Problem: https://leetcode.com/problems/binary-tree-level-order-traversal/description/
3+
Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level).
4+
=end
5+
6+
#Solution (Using BFS/Queue)
7+
8+
# Definition for a binary tree node.
9+
# class TreeNode
10+
# attr_accessor :val, :left, :right
11+
# def initialize(val = 0, left = nil, right = nil)
12+
# @val = val
13+
# @left = left
14+
# @right = right
15+
# end
16+
# end
17+
# @param {TreeNode} root
18+
# @return {Integer[][]}
19+
def level_order(root)
20+
val_list = []
21+
return [] if root.nil?
22+
queue = [root] # Queue.new can be used instead of List for efficiency of insertion
23+
24+
until queue.empty?
25+
level = []
26+
size = queue.length
27+
28+
size.times do
29+
node = queue.pop()
30+
level.append(node.val)
31+
queue.unshift(node.left) if node.left
32+
queue.unshift(node.right) if node.right
33+
end
34+
val_list.append(level)
35+
end
36+
val_list
37+
end

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /