508. 出现次数最多的子树元素和
class Solution(object):
def __init__(self):
self.mydict = {}
# 1、dfs求每个节点的子树和,并用字典统计
# 2、遍历字典,按values域的大小降序排:mylist = sorted(mydict.items(), key = lambda x: x[1], reverse=True)
# 3、遍历mylist,res中append字典的第一个item的values域,要是后面的values域长度跟首item相同,则res继续append;
# 4、return res
def findFrequentTreeSum(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
if not root:
return []
if not root.left and not root.right:
return [root.val]
self.getTheTreeSum(root)
print(self.mydict)
mylist = sorted(self.mydict.items(), key=lambda x: x[1], reverse=True)
maxsum = mylist[0][1]
res = []
for item in mylist:
if item[1] == maxsum:
res.append(item[0])
else:
break
return res
# dfs求每个节点的子树和
def getTheTreeSum(self, root):
temp = self.getCurSum(root)
if temp in self.mydict.keys():
self.mydict[temp] += 1
else:
self.mydict[temp] = 1
if root.left:
self.getTheTreeSum(root.left)
if root.right:
self.getTheTreeSum(root.right)
# dfs求树节点的值域和,参数是根节点
def getCurSum(self, node):
if not node:
return 0
if not node.left and not node.right:
return node.val
return self.getCurSum(node.left) + node.val + self.getCurSum(node.right)