Skip to content

Commit

Permalink
feat: Added two-three more solutions in Trees
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulravindran0108 committed Feb 20, 2017
1 parent c85d86c commit 4b4a6ef
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 0 deletions.
3 changes: 3 additions & 0 deletions SUMMARY.md
Expand Up @@ -209,6 +209,9 @@
* [Minimum Height Trees](trees/minimum_height_trees.md)
* [Path Sum 3](trees/path_sum_3.md)
* [Delete Node in BST](trees/delete_node_in_bst.md)
* [Most Frequent Subtree Sum](trees/most_frequent_subtree_sum.md)
* [Find Bottom Left Tree Value](trees/find_bottom_left_tree_value.md)
* [Find Largest Value in Each Row](trees/find_largest_value_in_each_row.md)
* [Hackerrank](hackerrank.md)
* [Fibonacci Modified](hackerrank/fibonacci_modified_hr.md)
* [Maximum Subarray](hackerrank/maximum_subarray.md)
Expand Down
63 changes: 63 additions & 0 deletions trees/find_bottom_left_tree_value.md
@@ -0,0 +1,63 @@
## Find Bottom Left Tree Value

Given a binary tree, find the leftmost value in the last row of the tree.

```
Example 1:
Input:
2
/ \
1 3
Output:
1
```

```
Example 2:
Input:
1
/ \
2 3
/ / \
4 5 6
/
7
Output:
7
```

Note: You may assume the tree (i.e., the given root node) is not NULL.

### Solution

```python
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution(object):
def findBottomLeftValue(self, root):
"""
:type root: TreeNode
:rtype: int
"""
res = [root.val, 0]
self.dfs(root, 0, res)
return res[0]

def dfs(self, root, level, res):
if root is None:
return
if root.left is None and root.right is None and (level == 0 or level > res[1]):
res[0], res[1] = root.val, level

self.dfs(root.left, level + 1, res)
self.dfs(root.right, level + 1, res)
```
48 changes: 48 additions & 0 deletions trees/find_largest_value_in_each_row.md
@@ -0,0 +1,48 @@
## Find Largest Value in Each row

You need to find the largest value in each row of a binary tree.

```
Example:
Input:
1
/ \
3 2
/ \ \
5 3 9
Output: [1, 3, 9]
```

### Solution

```python
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
import sys
from collections import defaultdict

class Solution(object):
def largestValues(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
res = defaultdict(lambda: -sys.maxint)

self.dfs(res, root, 0)
return [res[key] for key in sorted(res)]

def dfs(self, res, root, level):
if root is None:
return
res[level] = max(res[level],root.val)
self.dfs(res, root.left, level + 1)
self.dfs(res, root.right, level + 1)

```
59 changes: 59 additions & 0 deletions trees/most_frequent_subtree_sum.md
@@ -0,0 +1,59 @@
## Most Frequent Subtree Sum

Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself). So what is the most frequent subtree sum value? If there is a tie, return all the values with the highest frequency in any order.

```
Examples 1
Input:
5
/ \
2 -3
return [2, -3, 4], since all the values happen only once, return all of them in any order.
Examples 2
Input:
5
/ \
2 -5
return [2], since 2 happens twice, however -5 only occur once.
```

### Solution

```python
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

from collections import Counter

class Solution(object):
def findFrequentTreeSum(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
if root is None:
return list()

res = Counter()
self.dfs(res, root)
most_freq = res.most_common(1)[0][1]

return [x[0] for x in res.most_common() if x[1] == most_freq]

def dfs(self, path, root):
if root is None:
return 0

l = self.dfs(path, root.left)
r = self.dfs(path, root.right)

path.update([l + r + root.val])

return l + r + root.val
```

0 comments on commit 4b4a6ef

Please sign in to comment.