To the Top
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 | 257 Views, 18978 Search Bots | 147 Words

Subscribe to Feed Burner | 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:


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
Tags: 每日算法题, 算法, 数据结构, 面试题, Daily Interview Problem, Data Structures and Algorithms, Computer Programming, Python, | English | Home Page | Cateogry: Computing | 257 Views, 18978 Search Bots | 147 Words Subscribe to Feed Burner

Related Articles

  1. [Daily Problem] Longest Increasing Subsequence
  2. Daily Interview Problem: Reconstrunct Binary Tree from Preorder and Inorder Traversals
  3. Daily Interview Problem: Merge Overlapping Intervals
  4. Algorithm Interview: Level Order Traversal of Binary Tree
  5. Sorting a list with 3 unique numbers
  6. Algorithm Interview: Make the Largest Number
  7. CPU Utilization
  8. Algorithm Interview: Permutations of numbers
  9. Daily Interview Problem: Spiral Traversal of Grid
  10. Find Missing Numbers in an Array

Comments (0)

Your Email (Domain Part Not Exposed):

Your Comments:

Privately By Mail Colors More Smileys S x y @

Verification (Click Image 2 Refresh):

    Be the first one to comment this page !


Page Edited: October 30 2020 14:21:09 | RSS Subscription
How to Cook a Perfect Steak? | <meta name="robots" content="noindex, follow" />