Skip to content

Commit 1f714b5

Browse files
committed
tree questions leetcode
1 parent 5610628 commit 1f714b5

File tree

4 files changed

+39
-2
lines changed

4 files changed

+39
-2
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,14 @@ solution of many challenges of [HackerRank](https://www.hackerrank.com) and [Cod
193193

194194
#### Binary Trees
195195
1. [Invert Binary Tree](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/invert_binary_tree.rb)
196+
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)
197+
196198

197199
<a name="bst"/>
198200

199201
#### Binary Search Trees
200202
1. [Lowest Common Ancestor of BST](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/lowest_common_ancestor.rb)
203+
2. [Sorted Array to BST](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/sorted_array_to_bst.rb)
201204

202205
<a name="dp"/>
203206

@@ -259,7 +262,8 @@ solution of many challenges of [HackerRank](https://www.hackerrank.com) and [Cod
259262
24. [Climbing Stairs](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/climbing_stairs.rb)
260263
25. [Lowest Common Ancestor of BST](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/lowest_common_ancestor.rb)
261264
26. [Add Binary](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/add_binary.rb)
262-
265+
27. [Max Depth of Binary Tree](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/max_depth_of_binary_tree.rb)
266+
28. [Sorted Array to BST](https://github.com/kumar91gopi/Algorithms-and-Data-Structures-in-Ruby/blob/master/leetcode/grind75/sorted_array_to_bst.rb)
263267

264268
<a name="striver"/>
265269

leetcode/grind75/add_binary.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def add_binary(a, b)
1313
char_b = j>=0 ? b[j] : '0'
1414
if char_a =='0' and char_b=='0'
1515
char,carry = carry>0 ? [1,0] : [0,0]
16-
result="#{char}#{result}"
16+
result="#{char}#{result}" #instead of adding har in front you can append and reverse result in the end
1717
elsif (char_a=='0' and char_b=='1') or (char_a=='1' and char_b=='0')
1818
char,carry = carry>0 ? [0,1] : [1,0]
1919
result="#{char}#{result}"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=begin
2+
Given the root of a binary tree, return its maximum depth.
3+
A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
4+
=end
5+
6+
# Solution 1 (Recursion)
7+
def max_depth(root)
8+
return 0 if root.nil?
9+
return 1+[max_depth(root.left),max_depth(root.right)].max
10+
end
11+
12+
# Solution 2 (BFS)
13+
#TODO
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=begin
2+
Given an integer array nums where the elements are sorted in ascending order, convert it to a
3+
height-balanced binary search tree.
4+
=end
5+
6+
#Solution
7+
def sorted_array_to_bst(nums)
8+
l = 0
9+
r = nums.length-1
10+
create_bst(nums,l,r)
11+
end
12+
13+
def create_bst(nums,l,r)
14+
return nil if l>r
15+
mid = (l+r)>>1
16+
root = TreeNode.new(nums[mid])
17+
root.left = create_bst(nums,l,mid-1)
18+
root.right = create_bst(nums,mid+1,r)
19+
root
20+
end

0 commit comments

Comments
 (0)