File: root - text - article - 2020 - 02 - most-frequent-subtree-sum.txt
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 617 Views, 24556 Search Bots | 147 Words
| Browse | Archive
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Category: Computing | 617 Views, 24556 Search Bots | 147 Words
| Browse | Archive
Hi, here's your problem today. This problem was recently asked by LinkedIn:
Given a binary tree, find the most frequent subtree sum.
Example:
The above tree has 3 subtrees. The root node with 3, and the 2 leaf nodes, which gives us a total of 3 subtree sums. The root node has a sum of 1 (3 + 1 + -3), the left leaf node has a sum of 1, and the right leaf node has a sum of -3. Therefore the most frequent subtree sum is 1.
If there is a tie between the most frequent sum, you can return any one of them.
Here's some starter code for the problem:
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Cateogry: Computing | 617 Views, 24556 Search Bots | 147 Words Given a binary tree, find the most frequent subtree sum.
Example:
3
/ \
1 -3
The above tree has 3 subtrees. The root node with 3, and the 2 leaf nodes, which gives us a total of 3 subtree sums. The root node has a sum of 1 (3 + 1 + -3), the left leaf node has a sum of 1, and the right leaf node has a sum of -3. Therefore the most frequent subtree sum is 1.
If there is a tie between the most frequent sum, you can return any one of them.
Here's some starter code for the problem:
class Node():
def __init__(self, value, left=None, right=None):
self.val = value
self.left = left
self.right = right
def most_freq_subtree_sum(root):
# fill this in.
root = Node(3, Node(1), Node(-3))
print(most_freq_subtree_sum(root))
# 1
Related Articles
- Daily Interview Problem: Longest Substring With K Distinct Characters
- Daily Interview Problem: Sort Colors
- Algorithm Interview: Make the Largest Number
- Daily Interview Problem: Given two arrays, write a function to compute their intersection.
- Skip the readings, focus on problems. And use all the hints!
- [Daily Problem] Add two numbers as a linked list
- Algorithm Interview Question: Find the Single Element in an Array of Duplicates
- Daily Interview Problem: Minimum Removals for Valid Parenthesis
- Longest Substring Without Repeating Characters
- Daily Interview Problem: Get all Values at a Certain Height in a Binary Tree
©2006~2024 SteakOverCooked - 0.00872 Seconds(s) - 2300.041 KB/s - 25 Online Memory: 492.65 KB
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
How to Cook a Perfect Steak? | <meta name="robots" content="noindex, follow" />
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
Comments (0)
Read & Write - Normal - Mini - Post - All Comments - Statistics
Be the first one to comment this page !