From 4b4a6ef34e3d1b76e763583a2d8b96314fc4e2cc Mon Sep 17 00:00:00 2001 From: Rahul Ravindran Date: Sun, 19 Feb 2017 16:12:10 -0800 Subject: [PATCH] feat: Added two-three more solutions in Trees --- SUMMARY.md | 3 ++ trees/find_bottom_left_tree_value.md | 63 +++++++++++++++++++++++++ trees/find_largest_value_in_each_row.md | 48 +++++++++++++++++++ trees/most_frequent_subtree_sum.md | 59 +++++++++++++++++++++++ 4 files changed, 173 insertions(+) create mode 100644 trees/find_bottom_left_tree_value.md create mode 100644 trees/find_largest_value_in_each_row.md create mode 100644 trees/most_frequent_subtree_sum.md diff --git a/SUMMARY.md b/SUMMARY.md index 4df733a..ad2acc6 100644 --- a/SUMMARY.md +++ b/SUMMARY.md @@ -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) diff --git a/trees/find_bottom_left_tree_value.md b/trees/find_bottom_left_tree_value.md new file mode 100644 index 0000000..e090259 --- /dev/null +++ b/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) +``` diff --git a/trees/find_largest_value_in_each_row.md b/trees/find_largest_value_in_each_row.md new file mode 100644 index 0000000..aef8fc5 --- /dev/null +++ b/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) + +``` diff --git a/trees/most_frequent_subtree_sum.md b/trees/most_frequent_subtree_sum.md new file mode 100644 index 0000000..0e36482 --- /dev/null +++ b/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 +```