文件: root - text - article - 2020 - 02 - most-frequent-subtree-sum.txt
标签: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | 英文 | 主页 | 类别: 计算机科学 | 599 次阅读, 24098 次搜索 | 147 个单词
| 浏览 | 博客存档
标签: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | 英文 | 主页 | 类别: 计算机科学 | 599 次阅读, 24098 次搜索 | 147 个单词
| 浏览 | 博客存档
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:
标签: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | 英文 | 主页 | 类别: 计算机科学 | 599 次阅读, 24098 次搜索 | 147 个单词 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
猜您喜欢...
- Kaprekar
- [Daily Problem] Remove k-th Last Element From Linked List
- Daily Interview Question: Create a Simple Calculator
- Daily Interview Problem: Sort Colors
- Daily Interview Problem: Validate Binary Search Tree
- Daily Interview Puzzle: Falling Dominoes
- CVE-2015-8874 - cPanel EasyApache Vulnerabilities
- Daily Interview Problem: Circle of Chained Words
- Reverse a Directed Graph
- Daily Interview Problem: Longest Substring With K Distinct Characters
©2006~2024 牛排过熟 - 0.02081 秒 - 924.413 KB/s - 28 在线 内存: 492.47 KB
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - 服务器 PHP 版本号: 7.4.33
牛排怎么做才好吃? | <meta name="机器人" content="不索引, 跟踪" />
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - 服务器 PHP 版本号: 7.4.33
评论 (0)
读写完全 - 一般 - 最小 - 表格 - 所有评论 - 统计
当前页暂时没有评论。