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

猜您喜欢...

  1. Daily Interview Problem: Count Number of Unival Subtrees
  2. Progess made
  3. Daily Interview Puzzle: Intersection of Linked Lists
  4. Binary Tree Level with Minimum Sum
  5. Daily Interview Question: Find Cycles in a Graph
  6. Multitasking
  7. ImageMagick Vulnerabilities -  CVE-2016–3714
  8. Making a Height Balanced Binary Search Tree
  9. Daily Interview Question: Edit Distance
  10. Algorithm Interview: String Compression

评论 (0)

    当前页暂时没有评论。


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