<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實戰(zhàn)437:路徑總和 III

          共 2270字,需瀏覽 5分鐘

           ·

          2021-11-15 02:49

          算法的重要性,我就不多說了吧,想去大廠,就必須要經(jīng)過基礎(chǔ)知識和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做?路徑總和 III,我們先來看題面:
          https://leetcode-cn.com/problems/path-sum-iii/

          Given the root of a binary tree and an integer targetSum, return the number of paths where the sum of the values along the path equals targetSum.


          The path does not need to start or end at the root or a leaf, but it must go downwards (i.e., traveling only from parent nodes to child nodes).



          給定一個二叉樹的根節(jié)點 root ,和一個整數(shù) targetSum ,求該二叉樹里節(jié)點值之和等于 targetSum 的 路徑 的數(shù)目。
          路徑 不需要從根節(jié)點開始,也不需要在葉子節(jié)點結(jié)束,但是路徑方向必須是向下的(只能從父節(jié)點到子節(jié)點)。

          示例

          解題

          https://www.shuzhiduo.com/A/D854DXRp5E/

          路徑的開頭可以不是根節(jié)點,結(jié)束也可以不是葉子節(jié)點,是不是有點復(fù)雜?
          如果問題是這樣:找出以根節(jié)點為開始,任意節(jié)點可作為結(jié)束,且路徑上的節(jié)點和為 sum 的路徑的個數(shù);
          是不是前序遍歷一遍二叉樹就可以得到所有這樣的路徑?是的;
          如果這個問題解決了,那么原問題可以分解成多個這個問題;
          是不是和數(shù)線段是同一個問題,只不過線段變成了二叉樹;
          在解決了以根節(jié)點開始的所有路徑后,就要找以根節(jié)點的左孩子和右孩子開始的所有路徑,三個節(jié)點構(gòu)成了一個遞歸結(jié)構(gòu);


          /**
          ?* Definition for a binary tree node.
          ?* public class TreeNode {
          ?* int val;
          ?* TreeNode left;
          ?* TreeNode right;
          ?* TreeNode(int x) { val = x; }
          ?* }
          ?*/

          class?Solution?{
          ???/**
          ?????* 求以 root 為根的二叉樹,所有和為 sum 的路徑;
          ?????* 路徑的開頭不一定是 root,結(jié)尾也不一定是葉子節(jié)點;
          ?????* @param?root
          ?????* @param?sum
          ?????* @return
          ?????*/

          ????public?int?pathSum(TreeNode root, int?sum)?{
          ?
          ????????if?(root == null) {
          ????????????return?0;
          ????????}
          ?
          ????????return?paths(root, sum)
          ????????????????+ pathSum(root.left, sum)
          ????????????????+ pathSum(root.right, sum);
          ????}
          ?
          ????private?int?paths(TreeNode root, int?sum)?{
          ?
          ????????if?(root == null) {
          ????????????return?0;
          ????????}
          ?
          ????????int?res = 0;
          ????????if?(root.val == sum) {
          ????????????res += 1;
          ????????}
          ?
          ????????res += paths(root.left, sum - root.val);
          ????????res += paths(root.right, sum - root.val);
          ?
          ????????return?res;
          ????}
          }


          好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動力 。

          上期推文:

          LeetCode1-420題匯總,希望對你有點幫助!

          LeetCode刷題實戰(zhàn)421:數(shù)組中兩個數(shù)的最大異或值

          LeetCode刷題實戰(zhàn)422:有效的單詞方塊

          LeetCode刷題實戰(zhàn)423:從英文中重建數(shù)字

          LeetCode刷題實戰(zhàn)424:替換后的最長重復(fù)字符

          LeetCode刷題實戰(zhàn)425:單詞方塊

          LeetCode刷題實戰(zhàn)426:將二叉搜索樹轉(zhuǎn)化為排序的雙向鏈表

          LeetCode刷題實戰(zhàn)427:建立四叉樹

          LeetCode刷題實戰(zhàn)428:序列化和反序列化 N 叉樹

          LeetCode刷題實戰(zhàn)429:N 叉樹的層序遍歷

          LeetCode刷題實戰(zhàn)430:扁平化多級雙向鏈表

          LeetCode刷題實戰(zhàn)431:將 N 叉樹編碼為二叉樹

          LeetCode刷題實戰(zhàn)432:全 O(1) 的數(shù)據(jù)結(jié)構(gòu)

          LeetCode刷題實戰(zhàn)433:最小基因變化

          LeetCode刷題實戰(zhàn)434:字符串中的單詞數(shù)

          LeetCode刷題實戰(zhàn)435:無重疊區(qū)間

          LeetCode刷題實戰(zhàn)436:尋找右區(qū)間

          瀏覽 57
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  自拍偷拍中文 | 青青草区视频 | 一级黄片美女出来 | 波多野结衣av一区二区全免费观看 | 一色逼毛 |