129. 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.

For example,

    1
   / \
  2   3

The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.

Return the sum = 12 + 13 = 25.

一刷
題解:DFS。leaf的左右子節(jié)點均為空。
Time Complexity - O(n)父叙, Space Complexity - O(n)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int sumNumbers(TreeNode root) {
        return sum(root, 0);
    }
    
    public int sum(TreeNode n, int s){
        if(n == null) return 0;
        if(n.right == null && n.left == null) return s*10 + n.val;
        return sum(n.left, s*10 + n.val) + sum(n.right, s*10 + n.val);
    }
}

二刷:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    int total = 0;
    public int sumNumbers(TreeNode root) {
        if(root == null) return 0;
        sum(0, root);
        return total;
    }
    
    public void sum(int prev, TreeNode root){
        if(root == null) return;
        if(root.left == null && root.right == null) {
            total += prev*10 + root.val;
            return;
        }
        sum(prev*10 + root.val, root.left);
        sum(prev*10 + root.val, root.right);
    }
}

三刷
注意要判斷葉子,否則[1,2]這種結(jié)構(gòu)會返回1+12

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int sumNumbers(TreeNode root) {
        return sumNumbers(root, 0);
    }
    
    public int sumNumbers(TreeNode root, int sum){
        if(root == null) return 0;
        if(root.left == null && root.right == null) return sum*10 + root.val;
        return sumNumbers(root.left, 10*sum + root.val) + sumNumbers(root.right, 10*sum + root.val);
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末讥裤,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子松邪,更是在濱河造成了極大的恐慌坞琴,老刑警劉巖哨查,帶你破解...
    沈念sama閱讀 218,036評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件逗抑,死亡現(xiàn)場離奇詭異,居然都是意外死亡寒亥,警方通過查閱死者的電腦和手機邮府,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來溉奕,“玉大人褂傀,你說我怎么就攤上這事〖忧冢” “怎么了仙辟?”我有些...
    開封第一講書人閱讀 164,411評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長鳄梅。 經(jīng)常有香客問我叠国,道長,這世上最難降的妖魔是什么戴尸? 我笑而不...
    開封第一講書人閱讀 58,622評論 1 293
  • 正文 為了忘掉前任粟焊,我火速辦了婚禮,結(jié)果婚禮上孙蒙,老公的妹妹穿的比我還像新娘项棠。我一直安慰自己,他們只是感情好挎峦,可當我...
    茶點故事閱讀 67,661評論 6 392
  • 文/花漫 我一把揭開白布香追。 她就那樣靜靜地躺著,像睡著了一般坦胶。 火紅的嫁衣襯著肌膚如雪透典。 梳的紋絲不亂的頭發(fā)上歪玲,一...
    開封第一講書人閱讀 51,521評論 1 304
  • 那天,我揣著相機與錄音掷匠,去河邊找鬼滥崩。 笑死,一個胖子當著我的面吹牛讹语,可吹牛的內(nèi)容都是我干的钙皮。 我是一名探鬼主播,決...
    沈念sama閱讀 40,288評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼顽决,長吁一口氣:“原來是場噩夢啊……” “哼短条!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起才菠,我...
    開封第一講書人閱讀 39,200評論 0 276
  • 序言:老撾萬榮一對情侶失蹤茸时,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后赋访,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體可都,經(jīng)...
    沈念sama閱讀 45,644評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,837評論 3 336
  • 正文 我和宋清朗相戀三年蚓耽,在試婚紗的時候發(fā)現(xiàn)自己被綠了渠牲。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,953評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡步悠,死狀恐怖签杈,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情鼎兽,我是刑警寧澤答姥,帶...
    沈念sama閱讀 35,673評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站谚咬,受9級特大地震影響鹦付,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜序宦,卻給世界環(huán)境...
    茶點故事閱讀 41,281評論 3 329
  • 文/蒙蒙 一睁壁、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧互捌,春花似錦潘明、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至腌巾,卻和暖如春遂填,著一層夾襖步出監(jiān)牢的瞬間铲觉,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評論 1 269
  • 我被黑心中介騙來泰國打工吓坚, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留撵幽,地道東北人。 一個月前我還...
    沈念sama閱讀 48,119評論 3 370
  • 正文 我出身青樓礁击,卻偏偏與公主長得像盐杂,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子哆窿,可洞房花燭夜當晚...
    茶點故事閱讀 44,901評論 2 355

推薦閱讀更多精彩內(nèi)容