页面顶部 Top
文件:  root - text - article - 2020 - 02 - most-frequent-subtree-sum.txt
标签: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | 英文 | 主页 | 类别: 计算机科学 | 259 次阅读, 19032 次搜索 | 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:


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
标签: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | 英文 | 主页 | 类别: 计算机科学 | 259 次阅读, 19032 次搜索 | 147 个单词 定阅此目录的博客

猜您喜欢...

  1. Daily Interview Question: Longest Sequence with Two Unique Numbers
  2. Daily Interview Problem: Reconstrunct Binary Tree from Preorder and Inorder Traversals
  3. Generate All IP Addresses
  4. Daily Interview Problem: Merge K Sorted Linked Lists
  5. Algorithm Interview: Permutations of numbers
  6. Daily Interview Problem: Full Binary Tree
  7. Non-decreasing Array with Single Modification
  8. Daily Interview Problem: Validate Binary Search Tree
  9. [Daily Problem] Course Prerequisites
  10. Find Pythagorean Triplets

评论 (0)

您的邮件 (不公开域名部分):

您的评论:

悄悄话(发邮件) 颜色 更多笑脸 S x y @

验证码 (点击图片更新):

    当前页暂时没有评论。


最后更新: October 30 2020 14:21:12 | RSS Subscription
牛排怎么做才好吃? | <meta name="机器人" content="不索引, 跟踪" />