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

猜您喜欢...

  1. Floor and Ceiling of a Binary Search Tree
  2. Algorithm Interview Question: Nth Fibonacci Number
  3. Daily Interview Problem: Buddy Strings
  4. Daily Interview Problem: Product of Array Except Self
  5. Most Frequent Subtree Sum
  6. [Daily Problem] Validate Balanced Parentheses
  7. Spreadsheet Column Title
  8. Algorithm Interview: Convert Roman Numerals to Decimal
  9. How to Play WAV music under DOS?
  10. Autorun.inf Virus Protection

评论 (0)

    当前页暂时没有评论。


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