零题:Algorithm(leetcode,withmindmap+allsolutions)300题(508)出现次数最多的子树元素1个主题描述2个解法概述(思维导图)3所有解1方案11)代码://方案1《self.后续遍历法》。//花费时间:14分钟。//思路://1)状态初始化:resMap=newMap()。//2)调用递归函数updateRootValByDfs(root)。//3)获取最常出现的子树元素和resMaxCount=getMaxCountByMap(resMap)。//4)如果当前值(即key)出现的次数为val和resMaxCount,//将val放入resList。//5)返回结果resList。varfindFrequentTreeSum=function(root){constupdateRootValByDfs=(curRoot=null)=>{//1)递归退出if(!curRoot){return;}const{左,右}=curRoot;如果(!left&&!right){如果(resMap.has(curRoot.val)){resMap.set(curRoot.val,resMap.get(curRoot.val)+1);}else{resMap.set(curRoot.val,1);}返回;}//2)递归体//2.1)先更新左节点的val。updateRootValByDfs(左);//2.2)然后更新右节点的val。updateRootValByDfs(右);//2.3)最后更新curRoot节点上的val。如果(左){curRoot.val+=(左.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);}};constgetMaxCountByMap=(map=newMap())=>{让resMaxCount=Number.NEGATIVE_INFINITY;for(const[key,val]ofmap){resMaxCount=Math.max(resMaxCount,val);}返回resMaxCount;};//边界(根据提示,下面的代码可以忽略)if(!root){return[];}//1)状态初始化:resMap=newMap()。让resMap=newMap();//2)调用递归函数updateRootValByDfs(root)。updateRootValByDfs(root);//3)获取最常出现的子树元素和resMaxCount=getMaxCountByMap(resMap)。让resMaxCount=getMaxCountByMap(resMap),resList=[];//4)如果当前值(即key)出现的次数为val和resMaxCount,//将val放入resList。for(const[key,val]ofresMap){if(val===resMaxCount){resList.push(key);}}//5)返回结果resList。returnresList;};2Scheme21)Code://Scheme2《官方.深度优先遍历方法(本质:和自己的scheme1大致相同)》。//参考://1)https://leetcode.cn/problems/most-frequent-subtree-sum/solution/chu-xian-ci-shu-zui-duo-de-zi-shu-yuan-s-kdjc///思路://1)状态初始化:esMap=newMap(),resMaxCount=Number.NEGATIVE_INFINITY。//2)调用递归函数dfs(root)。//3)如果当前值(即key)出现的次数为val和resMaxCount,//将val放入resList。//4)返回结果resList。varfindFrequentTreeSum=function(root){constdfs=(curRoot=null)=>{//1)递归退出。如果(!curRoot){返回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)返回当前总和值!!返回总和;};//1)状态初始化:esMap=newMap(),resMaxCount=Number.NEGATIVE_INFINITY。让resMap=newMap(),resMaxCount=Number.NEGATIVE_INFINITY;//2)调用递归函数dfs(root)。dfs(根);//3)如果当前值(即key)出现的次数为val和resMaxCount,//将val放入resList。让resList=[];for(const[key,val]ofresMap){if(val===resMaxCount){resList.push(key);}}//4)返回结果resList。返回资源列表;};四资源共享&更多1历史文章-概述文章名称解决阅读卷1.TwoSum(TwoSum)共3种类型2.7k+2。两数相加(AddTwoNumbers)总计42.7k+3。LongestSubstringWithoutRepeatingCharacters(最长子串无重复字符)3种2.6k+4。求两个正数数组的中位数(MedianofTwoSortedArrays)3类型2.8k+5。最长回文子串(LongestPalindromicSubstring)共4种2.8k+6。ZigZagConversion(之字折线转换)共2种1.9k+7。IntegerReverse(ReverseInteger)共2种2.4k+8。StringtoInteger(atoi)(StringtoInteger(atoi))共3种4.2k+9。回文数(PalindromeNumber)共3种4.3k+11.ContainerWithMostWater(容器最多水)共5种4.0k+12。IntegertoRoman(整数转罗马)共3种3.2k+13。罗马数字(RomantoInteger)共3种3.8k+14。LongestCommonPrefix(最长公共前缀)一共有4种3.0k+15。三数之和(3Sum)一共有3种60.7k+16。最近三数之和(3SumClosest)一共有3种4.7k+17。电话号码的字母组合(LetterCombinationsofaPhoneNumber)共3种3.1k+18。四数之和(4Sum)一共4种11.5k+19。删除链表倒数第二个N节点(RemoveNthNodeFromEndofList)共4种1.2k+20。ValidParentheses有2种类型1.8k+21。MergeTwoSortedLists有3种类型1.2k+22。生成括号(GenerateParen论文)共4种1.1k+23。合并K个升序链表(MergekSortedLists)共4种0.9k+24。链表中的两两交换节点(SwapNodesinPairs)共3种0.5k+25。K-Group中的反向节点(ReverseNodesink-Group)有5种类型1.3k+26。RemoveDuplicatesfromSortedArray(从排序数组中删除重复项)4种1.3k+27。移除元素(RemoveElement)共4种0.4k+28。实现strStr()(ImplementstrStr())共5种0.8k+29。两个整数相除(DivideTwoIntegers)共4种0.6k+30。String(SubstringwithConcatenationofAllWords)total3types0.6k+31.NextPermutation(NextPermutation)total2types0.8k+32.LongestValidParentheses(最长有效括号)共2种1.4k+33。Searchrotation在RotatedSortedArray1.0k+34中有3种Search。FindFirstandLastPositionofElementinSortedArray0.5k+35有3种类型。SearchInsertPosition(搜索插入位置)共3种0.3k+36。有效数独(ValidSudoku)共1种0.6k+38。出现顺序(CountandSay)共5种1.1k+39。3种组合和(CombinationSum)1.4k+40。2种的CombinationSumII(组合和II)1.6k+41。3种缺失第一个正数(FirstMissingPositive)1.2k+53。最大子数组和(MaximumSuarray)一共3种0.3k+88。合并两个有序数组(MergeSortedArray)共3个0.4k+102。二叉树层序遍历(BinaryTreeLevelOrderTraversal)共3种0.4k+146。LRU缓存(LRUCache)一共2种0.5k+160。两个链表的交集(IntersectionofTwoLinkedLists)共2种0.1k+200。岛屿数(NumberofIslands)共4种0.1k+206。反向链表(ReverseLinkedList)共3种1.0k+215。KthLargestElementinthearray(KthLargestElementinanArray)一共3种0.5k+236。二叉树的最近共同祖先(LowestCommonAncestorofaBinaryTree)一共3种0.1k+2119。双重反转后的数字)共2类型0.3k+2120。ExecutionofAllSuffixInstructionsStayinginaGrid(ExecutionofAllSuffixInstructionsStayinginaGrid)共1种0.4k+2124。CheckifallA'sappearbeforeB(CheckifAllA'sAppearsBeforeAllB's)共4种0.4k+2125。Bank3种激光束数0.3k+2126。毁灭小行星2类型1.6k+2129。大写标题TheTitle)共2种0.6k+2130。链表的最大孪生和(MaximumTwinSumofaLinkedList)一共2种0.6k+2133。Checkifeveryrowandcolumncontainsallintegers(CheckifEveryRowandColumnContainsAllNumbers)共1种0.6k+2博主介绍代码农三少,致力于编写极简但完整的问题解决方案(算法)的博主,专注一道题多解,结构化思维,欢迎一起刷LeetCode~
