Skip to content

Commit a59e4cb

Browse files
committed
Tree tarversals
1 parent b11b665 commit a59e4cb

File tree

5 files changed

+45
-6
lines changed

5 files changed

+45
-6
lines changed

Diff for: README.md

+10-6
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,16 @@ solution of many challenges of [Leetcode](https://leetcode.com/), [Exercism](htt
199199
<a name="binary_tree"/>
200200

201201
#### Binary Trees
202-
1. [Invert Binary Tree](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/invert_binary_tree.rb)
203-
2. [Height of Binary Tree](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/max_depth_of_binary_tree.rb)
204-
3. [Same Tree](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/same_tree.rb)
205-
4. [Balanced Binary tree](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/balanced_binary_tree.rb)
206-
5. [Symmetric Tree](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/symmetric_tree.rb)
207-
6. [Subtree of Another Tree](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/subtree_of_another_tree.rb)
202+
1. [Tree Node](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/trees/tree_node.rb)
203+
2. [In-order Traversal](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/trees/inorder_traversal.rb)
204+
3. [Pre-order Traversal](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/trees/preorder_traversal.rb)
205+
4. [Post-order Traversal](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/trees/postorder_traversal.rb)
206+
5. [Invert Binary Tree](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/invert_binary_tree.rb)
207+
6. [Height of Binary Tree](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/max_depth_of_binary_tree.rb)
208+
7. [Same Tree](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/same_tree.rb)
209+
8. [Balanced Binary tree](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/balanced_binary_tree.rb)
210+
9. [Symmetric Tree](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/symmetric_tree.rb)
211+
10. [Subtree of Another Tree](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/subtree_of_another_tree.rb)
208212

209213

210214
<a name="bst"/>

Diff for: trees/inorder_traversal.rb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# In-Order Traversal follows left-->root-->right
2+
3+
def inorder_traversal(root,val_list=[])
4+
return [] if root.nil?
5+
inorder_traversal(root.left,val_list)
6+
val_list.append(root.val)
7+
inorder_traversal(root.right,val_list)
8+
val_list
9+
end

Diff for: trees/postorder_traversal.rb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Post-Order Traversal follows left-->right-->root
2+
3+
def postorder_traversal(root, val_list=[])
4+
return [] if root.nil?
5+
inorder_traversal(root.left,val_list)
6+
inorder_traversal(root.right,val_list)
7+
val_list.append(root.val)
8+
val_list
9+
end

Diff for: trees/preorder_traversal.rb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Pre-Order Traversal follows root-->left-->right
2+
3+
def preorder_traversal(root, val_list=[])
4+
return [] if root.nil?
5+
val_list.append(root.val)
6+
inorder_traversal(root.left)
7+
inorder_traversal(root.right)
8+
end

Diff for: trees/tree_node.rb

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Node for holding data and left,right pointers in tree
2+
3+
class Node
4+
def initialize(val:nil, left:nil, right:nil)
5+
@val = val
6+
@left = left
7+
@right = right
8+
end
9+
end

0 commit comments

Comments
 (0)