Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added two-three more solutions in Trees
- Loading branch information
1 parent
c85d86c
commit 4b4a6ef
Showing
4 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |