头图

Zero title: Algorithm (leetcode, with mind map + all solutions) of 300 questions (508) with the most frequent subtree elements and

a topic description

题目描述
题目描述

Two solutions overview (mind map)

思维导图

All three solutions

1 Scenario 1

1) Code:

 // 方案1 “自己。后续遍历法”。
// 用时:14分钟。

// 思路:
// 1)状态初始化:resMap = new Map() 。
// 2)调用递归函数 updateRootValByDfs(root) 。
// 3)求得 出现次数最多的子树元素和 resMaxCount = getMaxCountByMap(resMap) 。
// 4)若 当前值(即 key )出现的次数 val 与 resMaxCount,
// 则 将 val 放入 resList 中。
// 5)返回结果 resList 。
var findFrequentTreeSum = function(root) {
    const updateRootValByDfs = (curRoot = null) => {
        // 1)递归出口
        if (!curRoot) {
            return;
        }
        const {left, right} = curRoot;
        if (!left && !right) {
            if (resMap.has(curRoot.val)) {
                resMap.set(curRoot.val, resMap.get(curRoot.val) + 1);
            }
            else {
                resMap.set(curRoot.val, 1);
            }
            return;
        }

        // 2)递归主体
        // 2.1)先更新 left 节点上的 val 。
        updateRootValByDfs(left);
        // 2.2)接着更新 right 节点上的 val 。
        updateRootValByDfs(right);
        // 2.3)最后更新 curRoot 节点上的 val 。
        if (left) {
            curRoot.val += (left.val);
        }
        if (right) {
            curRoot.val += (right.val);
        }

        if (resMap.has(curRoot.val)) {
            resMap.set(curRoot.val, resMap.get(curRoot.val) + 1);
        }
        else {
            resMap.set(curRoot.val, 1);
        }
    };
    const getMaxCountByMap = (map = new Map()) => {
        let resMaxCount = Number.NEGATIVE_INFINITY;

        for (const [key, val] of map) {
            resMaxCount = Math.max(resMaxCount, val);
        }

        return resMaxCount;
    };

    // 边界(根据提示,如下代码可忽略)
    if (!root) {
        return [];
    }

    // 1)状态初始化:resMap = new Map() 。
    let resMap = new Map();

    // 2)调用递归函数 updateRootValByDfs(root) 。
    updateRootValByDfs(root);

    // 3)求得 出现次数最多的子树元素和 resMaxCount = getMaxCountByMap(resMap) 。
    let resMaxCount = getMaxCountByMap(resMap),
        resList = [];
    
    // 4)若 当前值(即 key )出现的次数 val 与 resMaxCount,
    // 则 将 val 放入 resList 中。
    for (const [key, val] of resMap) {
        if (val === resMaxCount) {
            resList.push(key);
        }
    }

    // 5)返回结果 resList 。
    return resList;
};

2 Option 2

1) Code:

 // 方案2 “官方。深度优先遍历法(本质:跟自己的方案1大体上是一样的)”。
// 参考:
// 1)https://leetcode.cn/problems/most-frequent-subtree-sum/solution/chu-xian-ci-shu-zui-duo-de-zi-shu-yuan-s-kdjc/

// 思路:
// 1)状态初始化:esMap = new Map(), resMaxCount = Number.NEGATIVE_INFINITY 。
// 2)调用递归函数 dfs(root) 。
// 3)若 当前值(即 key )出现的次数 val 与 resMaxCount,
// 则 将 val 放入 resList 中。
// 4)返回结果 resList 。
var findFrequentTreeSum = function(root) {
    const dfs = (curRoot = null) => {
        // 1)递归出口。
        if (!curRoot) {
            return 0;
        }

        // 2)递归主体。
        const {val, left, right} = curRoot,
            sum = val + dfs(left) + dfs(right);
        
        // 2.1)不断更新 resMap、resMaxCount 的值。
        resMap.set(sum, (resMap.get(sum) || 0) + 1);
        resMaxCount = Math.max(resMaxCount, resMap.get(sum));

        // 2.2)返回当前 sum 值!!
        return sum;
    };

    // 1)状态初始化:esMap = new Map(), resMaxCount = Number.NEGATIVE_INFINITY 。
    let resMap = new Map(),
        resMaxCount = Number.NEGATIVE_INFINITY;

    // 2)调用递归函数 dfs(root) 。
    dfs(root);

    // 3)若 当前值(即 key )出现的次数 val 与 resMaxCount,
    // 则 将 val 放入 resList 中。
    let resList = [];
    for (const [key, val] of resMap) {
        if (val === resMaxCount) {
            resList.push(key);
        }
    }

    // 4)返回结果 resList 。
    return resList;
};

Four resource sharing & more

1 Historical Articles - Overview

Article name solution reading volume
1. Two Sum 3 types in total 2.7k+
2. Add Two Numbers 4 types in total 2.7k+
3. Longest Substring Without Repeating Characters 3 types in total 2.6k+
4. Find the Median of Two Sorted Arrays 3 types in total 2.8k+
5. Longest Palindromic Substring 4 types in total 2.8k+
6. ZigZag Conversion 2 types in total 1.9k+
7. Reverse Integer 2 types in total 2.4k+
8. String to Integer (atoi) 3 types in total 4.2k+
9. Palindrome Number 3 types in total 4.3k+
11. Container With Most Water 5 in total 4.0k+
12. Integer to Roman 3 types in total 3.2k+
13. Roman to Integer 3 types in total 3.8k+
14. Longest Common Prefix 4 types in total 3.0k+
15. The Sum of Three Numbers (3Sum) 3 types in total 60.7k+
16. 3Sum Closest 3 types in total 4.7k+
17. Letter Combinations of a Phone Number 3 types in total 3.1k+
18. The sum of four numbers (4Sum) 4 types in total 11.5k+
19. Remove Nth Node From End of List 4 types in total 1.2k+
20. Valid Parentheses 2 types in total 1.8k+
21. Merge Two Sorted Lists 3 types in total 1.2k+
22. Generate Parentheses 4 types in total 1.1k+
23. Merge k Sorted Lists 4 types in total 0.9k+
24. Swap Nodes in Pairs 3 types in total 0.5k+
25. Reverse Nodes in k-Group 5 in total 1.3k+
26. Remove Duplicates from Sorted Array 4 types in total 1.3k+
27. Remove Element 4 types in total 0.4k+
28. Implement strStr() (Implement strStr()) 5 in total 0.8k+
29. Divide Two Integers 4 types in total 0.6k+
30. Substring with Concatenation of All Words 3 types in total 0.6k+
31. Next Permutation 2 types in total 0.8k+
32. Longest Valid Parentheses 2 types in total 1.4k+
33. Search in Rotated Sorted Array 3 types in total 1.0k+
34. Find First and Last Position of Element in Sorted Array 3 types in total 0.5k+
35. Search Insert Position 3 types in total 0.3k+
36. Valid Sudoku 1 in total 0.6k+
38. Count and Say 5 in total 1.1k+
39. Combination Sum 3 types in total 1.4k+
40. Combination Sum II 2 types in total 1.6k+
41. First Missing Positive 3 types in total 1.2k+
53. Maximum Subarray Sum (Maximum Subarray) 3 types in total 0.3k+
88. Merge Sorted Array 3 types in total 0.4k+
102. Binary Tree Level Order Traversal 3 types in total 0.4k+
146. LRU Cache (LRU Cache) 2 types in total 0.5k+
160. Intersection of Two Linked Lists 2 types in total 0.1k+
200. Number of Islands 4 types in total 0.1k+
206. Reverse Linked List 3 types in total 1.0k+
215. Kth Largest Element in an Array 3 types in total 0.5k+
236. Lowest Common Ancestor of a Binary Tree 3 types in total 0.1k+
2119. A Number After a Double Reversal 2 types in total 0.3k+
2120. Execution of All Suffix Instructions Staying in a Grid 1 in total 0.4k+
2124. Check if All A's Appears Before All B's 4 types in total 0.4k+
2125. Number of Laser Beams in a Bank 3 types in total 0.3k+
2126. Destroying Asteroids 2 types in total 1.6k+
2129. Capitalize the Title 2 types in total 0.6k+
2130. Maximum Twin Sum of a Linked List 2 types in total 0.6k+
2133. Check if Every Row and Column Contains All Numbers 1 in total 0.6k+

刷题进度 - LeetCode:545 / 2678 、《剑指offer》:66 / 66

2 Introduction to bloggers

Code Farmer Sanshao, a blogger dedicated to writing minimalist but complete problem solutions (algorithms ).
Focus on one question, multiple solutions, structured thinking , welcome to brush through LeetCode ~


码农三少
54 声望8 粉丝