关于leetcode:508出现次数最多的子树元素和-算法leetcode附思维导图-全部解法300题

4次阅读

共计 4807 个字符,预计需要花费 13 分钟才能阅读完成。

零 题目:算法(leetcode,附思维导图 + 全副解法)300 题之(508)呈现次数最多的子树元素和

一 题目形容


二 解法总览(思维导图)

三 全副解法

1 计划 1

1)代码:

// 计划 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 计划 2

1)代码:

// 计划 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;
};

四 资源分享 & 更多

1 历史文章 – 总览

文章名称 解法 浏览量
1. 两数之和(Two Sum) 共 3 种 2.7 k+
2. 两数相加(Add Two Numbers) 共 4 种 2.7 k+
3. 无反复字符的最长子串(Longest Substring Without Repeating Characters) 共 3 种 2.6 k+
4. 寻找两个正序数组的中位数(Median of Two Sorted Arrays) 共 3 种 2.8 k+
5. 最长回文子串(Longest Palindromic Substring) 共 4 种 2.8 k+
6. Z 字形变换(ZigZag Conversion) 共 2 种 1.9 k+
7. 整数反转(Reverse Integer) 共 2 种 2.4 k+
8. 字符串转换整数 (atoi)(String to Integer (atoi)) 共 3 种 4.2 k+
9. 回文数(Palindrome Number) 共 3 种 4.3 k+
11. 盛最多水的容器(Container With Most Water) 共 5 种 4.0 k+
12. 整数转罗马数字(Integer to Roman) 共 3 种 3.2 k+
13. 罗马数字转整数(Roman to Integer) 共 3 种 3.8 k+
14. 最长公共前缀(Longest Common Prefix) 共 4 种 3.0 k+
15. 三数之和(3Sum) 共 3 种 60.7 k+
16. 最靠近的三数之和(3Sum Closest) 共 3 种 4.7 k+
17. 电话号码的字母组合(Letter Combinations of a Phone Number) 共 3 种 3.1 k+
18. 四数之和(4Sum) 共 4 种 11.5 k+
19. 删除链表的倒数第 N 个结点(Remove Nth Node From End of List) 共 4 种 1.2 k+
20. 无效的括号(Valid Parentheses) 共 2 种 1.8 k+
21. 合并两个有序链表(Merge Two Sorted Lists) 共 3 种 1.2 k+
22. 括号生成(Generate Parentheses) 共 4 种 1.1 k+
23. 合并 K 个升序链表(Merge k Sorted Lists) 共 4 种 0.9 k+
24. 两两替换链表中的节点(Swap Nodes in Pairs) 共 3 种 0.5 k+
25. K 个一组翻转链表(Reverse Nodes in k-Group) 共 5 种 1.3 k+
26. 删除有序数组中的反复项(Remove Duplicates from Sorted Array) 共 4 种 1.3 k+
27. 移除元素(Remove Element) 共 4 种 0.4 k+
28. 实现 strStr()(Implement strStr()) 共 5 种 0.8 k+
29. 两数相除(Divide Two Integers) 共 4 种 0.6 k+
30. 串联所有单词的子串(Substring with Concatenation of All Words) 共 3 种 0.6 k+
31. 下一个排列(Next Permutation) 共 2 种 0.8 k+
32. 最长无效括号(Longest Valid Parentheses) 共 2 种 1.4 k+
33. 搜寻旋转排序数组(Search in Rotated Sorted Array) 共 3 种 1.0k+
34. 在排序数组中查找元素的第一个和最初一个地位(Find First and Last Position of Element in Sorted Array) 共 3 种 0.5 k+
35. 搜寻插入地位(Search Insert Position) 共 3 种 0.3 k+
36. 无效的数独(Valid Sudoku) 共 1 种 0.6 k+
38. 外观数列(Count and Say) 共 5 种 1.1 k+
39. 组合总和(Combination Sum) 共 3 种 1.4 k+
40. 组合总和 II(Combination Sum II) 共 2 种 1.6 k+
41. 缺失的第一个负数(First Missing Positive) 共 3 种 1.2 k+
53. 最大子数组和(Maximum Subarray) 共 3 种 0.3k+
88. 合并两个有序数组(Merge Sorted Array) 共 3 种 0.4 k+
102. 二叉树的层序遍历(Binary Tree Level Order Traversal) 共 3 种 0.4 k+
146. LRU 缓存(LRU Cache) 共 2 种 0.5 k+
160. 相交链表(Intersection of Two Linked Lists) 共 2 种 0.1 k+
200. 岛屿数量(Number of Islands) 共 4 种 0.1 k+
206. 反转链表(Reverse Linked List) 共 3 种 1.0 k+
215. 数组中的第 K 个最大元素(Kth Largest Element in an Array) 共 3 种 0.5 k+
236. 二叉树的最近公共先人(Lowest Common Ancestor of a Binary Tree) 共 3 种 0.1 k+
2119. 反转两次的数字(A Number After a Double Reversal) 共 2 种 0.3 k+
2120. 执行所有后缀指令(Execution of All Suffix Instructions Staying in a Grid) 共 1 种 0.4 k+
2124. 查看是否所有 A 都在 B 之前(Check if All A’s Appears Before All B’s) 共 4 种 0.4 k+
2125. 银行中的激光束数量(Number of Laser Beams in a Bank) 共 3 种 0.3 k+
2126. 捣毁小行星(Destroying Asteroids) 共 2 种 1.6 k+
2129. 将题目首字母大写(Capitalize the Title) 共 2 种 0.6 k+
2130. 链表最大孪生和(Maximum Twin Sum of a Linked List) 共 2 种 0.6 k+
2133. 查看是否每一行每一列都蕴含全副整数(Check if Every Row and Column Contains All Numbers) 共 1 种 0.6 k+

2 博主简介

码农三少,一个致力于编写 极简、但齐全题解(算法 )的博主。
专一于 一题多解、结构化思维,欢送一起刷穿 LeetCode ~

正文完
 0