[leetcode]508. Most Frequent Subtree Sum二叉树中出现最多的值

遍历二叉树,用map记录sum出现的次数,每一个新的节点都统计一次。

遍历完就统计map中出现最多的sum

Map<Integer,Integer> map = new HashMap<>();
    public int[] findFrequentTreeSum(TreeNode root) {
        helper(root);
        int max = 0;
        List<Integer> list  = new ArrayList<>();
        for (int key : map.keySet()) {
            int v = map.get(key);
            if (v>max)
            {
                list.clear();
                list.add(key);
                max = v;
            }
            else if (v==max) list.add(key);
        }
        int[] res = new int[list.size()];
        for (int i = 0; i < res.length; i++) {
            res[i] = list.get(i);
        }
        return res;
    }
    public int helper(TreeNode root)
    {
        if (root==null) return 0;
        int cur = root.val;
        cur+= helper(root.left);
        cur+=helper(root.right);
        map.put(cur,map.getOrDefault(cur,0)+1);
        return cur;
    }

原文地址:https://www.cnblogs.com/stAr-1/p/8390478.html

时间: 2025-01-21 23:08:04

[leetcode]508. Most Frequent Subtree Sum二叉树中出现最多的值的相关文章

508. Most Frequent Subtree Sum (Medium)

Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself). So what is the most frequent

508. Most Frequent Subtree Sum 最频繁的子树和

[抄题]: Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself). So what is the most fre

508.&#160;Most Frequent Subtree Sum

class Solution { int count = 0; HashMap<Integer, Integer> map = new HashMap<>(); // map and count all must be here to be accessible from the functions below public int[] findFrequentTreeSum(TreeNode root) { subSum(root); List<Integer> re

LeetCode Most Frequent Subtree Sum

原题链接在这里:https://leetcode.com/problems/most-frequent-subtree-sum/description/ 题目: Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a node is defined as the sum of all the node values formed by the subtr

[LeetCode] Most Frequent Subtree Sum 出现频率最高的子树和

Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself). So what is the most frequent

[leetcode-508-Most Frequent Subtree Sum]

Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself). So what is the most frequent

[Swift]LeetCode508. 出现次数最多的子树元素和 | Most Frequent Subtree Sum

Given the root of a tree, you are asked to find the most frequent subtree sum. The subtree sum of a node is defined as the sum of all the node values formed by the subtree rooted at that node (including the node itself). So what is the most frequent

Leetcode: Binary Tree Inorder Traversal(二叉树中序遍历)

题目: Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 递归解法(C++): /** * Definition for binary tre

输出二叉树中路径上结点值之和为给定值的所有路径

#include<iostream>#include<vector>#include<algorithm>#include<stdint.h>using namespace std;#include<list>#include<map>#include<queue>struct node{ int val; node* left, *right; node(int _val) :val(_val), left(NULL),