<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)129:求根到葉子節(jié)點數(shù)字之和

          共 1737字,需瀏覽 4分鐘

           ·

          2020-12-21 15:11

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

          今天和大家聊的問題叫做?求根到葉子節(jié)點數(shù)字之和,我們先來看題面:
          https://leetcode-cn.com/problems/sum-root-to-leaf-numbers/

          Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.


          An example is the root-to-leaf path 1->2->3 which represents the number 123.


          Find the total sum of all root-to-leaf numbers.


          Note: A leaf is a node with no children.

          題意


          給定一個二叉樹,它的每個結(jié)點都存放一個 0-9 的數(shù)字,每條從根到葉子節(jié)點的路徑都代表一個數(shù)字。
          例如,從根到葉子節(jié)點路徑 1->2->3 代表數(shù)字 123。
          計算從根到葉子節(jié)點生成的所有數(shù)字之和。
          說明: 葉子節(jié)點是指沒有子節(jié)點的節(jié)點。


          樣例

          解題


          本體思路:采用層序遍歷+遞歸的方法進行求解。

          步驟一:構(gòu)建遞歸函數(shù)(root代表當前根節(jié)點,number代表從根節(jié)點到當前節(jié)點的值)

          步驟二:首先判斷根節(jié)點是否有子節(jié)點,如果有,將根節(jié)點number*10+子節(jié)點的值放入新的遞歸函數(shù)中,直到所子節(jié)點遍歷完畢,逐層返回所有值。

          class?Solution?{
          ????public?int?sumNumbers(TreeNode root) {
          ??????????if(root==null) {
          ????????????????return?0;
          ????????????}else?{
          ????????????????return?getNumber(root,root.val);
          ????????????}
          ????}
          ???public?int?getNumber(TreeNode root,int?number) {
          ????????if(root.left==null&&root.right==null) {
          ????????????return?number;
          ????????}
          ????????int?templeft=0;
          ????????int?tempright=0;
          ????????if(root.left!=null) {
          ????????????templeft=getNumber(root.left,root.left.val+number*10);
          ????????}
          ????????if(root.right!=null) {
          ????????????tempright=getNumber(root.right,root.right.val+number*10);
          ????????}
          ????????return?templeft+tempright;
          ????????
          ????}
          }


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

          上期推文:

          LeetCode1-120題匯總,希望對你有點幫助!
          LeetCode刷題實戰(zhàn)121:買賣股票的最佳時機
          LeetCode刷題實戰(zhàn)122:買賣股票的最佳時機 II
          LeetCode刷題實戰(zhàn)123:買賣股票的最佳時機 III
          LeetCode刷題實戰(zhàn)124:二叉樹中的最大路徑和
          LeetCode刷題實戰(zhàn)125:驗證回文串
          LeetCode刷題實戰(zhàn)126:單詞接龍 II
          LeetCode刷題實戰(zhàn)127:單詞接龍
          LeetCode刷題實戰(zhàn)128:最長連續(xù)序列


          瀏覽 21
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

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

          手機掃一掃分享

          分享
          舉報
          <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>
                  操出白浆视频 | 日产久久视频 | 成人H动漫精品一区二区无码软件 | 女人18毛片90分钟 | 麻豆操逼片 |