-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
52 lines (46 loc) · 1.31 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Collections.Generic;
using System.Linq;
namespace MostFrequentSubtreeSum
{
public class TreeNode
{
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int x) { val = x; }
}
class Program
{
static void Main(string[] args)
{
var a = new TreeNode(5);
var b = new TreeNode(2);
var c = new TreeNode(-3);
a.left = b;
a.right = c;
Console.WriteLine(FindFrequentTreeSum(a));
}
static int[] FindFrequentTreeSum(TreeNode root)
{
if(root == null) return new int[0];
var dict = new Dictionary<int, int>();
GetSum(root, dict);
int max = dict.Max(x => x.Value);
return dict.Where(x => x.Value == max).Select(x => x.Key).ToArray();
}
static int GetSum(TreeNode node, Dictionary<int, int> dict)
{
if (node == null) return 0;
int left = GetSum(node.left, dict);
int right = GetSum(node.right, dict);
int sum = node.val + left + right;
if (!dict.ContainsKey(sum))
dict[sum] = 1;
else
dict[sum]++;
return sum;
}
}
}