[LeetCode 129] Sum Root to Leaf Numbers (Medium)

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.

Example:

Input: [1,2,3]
    1
   / \
  2   3
Output: 25

Explanation:
The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.
Therefore, sum = 12 + 13 = 25.

Example 2:

Input: [4,9,0,5,1]
    4
   / \
  9   0
 / \
5   1
Output: 1026

Explanation:
The root-to-leaf path 4->9->5 represents the number 495.
The root-to-leaf path 4->9->1 represents the number 491.
The root-to-leaf path 4->0 represents the number 40.
Therefore, sum = 495 + 491 + 40 = 1026.

Solution:DFS, Recursion -- Top- down

  1. Top-down扔役,每次都把 parent的值 * 10 + current value朝捆,就是當(dāng)前節(jié)點(diǎn)的value
  2. 迭代函數(shù)維護(hù)2個變量:int parentSum, int[] result
  3. Base Case:
  if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {
            int currentNum = parentSum * 10 + root.val;
            result[0] += currentNum;
            return;
        }
  1. ParentSum * 10 + current Value傳入左右子節(jié)點(diǎn)的遞歸函數(shù)中繼續(xù)求解
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int sumNumbers(TreeNode root) {
        if (root == null) {
           return 0;
        }
        
        int[] result = { 0 };
        sumNumbers (root, 0, result);
        
        return result[0];
    }
    
    private void sumNumbers (TreeNode root, int parentSum, int[] result) {
        if (root == null) {
            return;
        }
        
        if (root.left == null && root.right == null) {
            int currentNum = parentSum * 10 + root.val;
            result[0] += currentNum;
            return;
        }
        
        sumNumbers (root.left, parentSum * 10 + root.val, result);
        sumNumbers (root.right, parentSum * 10 + root.val, result);
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市殿较,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,539評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件挽霉,死亡現(xiàn)場離奇詭異,居然都是意外死亡变汪,警方通過查閱死者的電腦和手機(jī)侠坎,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,594評論 3 396
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來裙盾,“玉大人实胸,你說我怎么就攤上這事》伲” “怎么了庐完?”我有些...
    開封第一講書人閱讀 165,871評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長鲤拿。 經(jīng)常有香客問我假褪,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,963評論 1 295
  • 正文 為了忘掉前任箍镜,我火速辦了婚禮慎陵,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘奈附。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,984評論 6 393
  • 文/花漫 我一把揭開白布饱须。 她就那樣靜靜地躺著,像睡著了一般台谊。 火紅的嫁衣襯著肌膚如雪蓉媳。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,763評論 1 307
  • 那天锅铅,我揣著相機(jī)與錄音酪呻,去河邊找鬼。 笑死盐须,一個胖子當(dāng)著我的面吹牛玩荠,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 40,468評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼阶冈,長吁一口氣:“原來是場噩夢啊……” “哼闷尿!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起女坑,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤填具,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后匆骗,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體灌旧,經(jīng)...
    沈念sama閱讀 45,850評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,002評論 3 338
  • 正文 我和宋清朗相戀三年绰筛,在試婚紗的時候發(fā)現(xiàn)自己被綠了枢泰。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,144評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡铝噩,死狀恐怖衡蚂,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情骏庸,我是刑警寧澤毛甲,帶...
    沈念sama閱讀 35,823評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站具被,受9級特大地震影響玻募,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜一姿,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,483評論 3 331
  • 文/蒙蒙 一七咧、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧叮叹,春花似錦艾栋、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,026評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至携冤,卻和暖如春悼粮,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背曾棕。 一陣腳步聲響...
    開封第一講書人閱讀 33,150評論 1 272
  • 我被黑心中介騙來泰國打工扣猫, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人睁蕾。 一個月前我還...
    沈念sama閱讀 48,415評論 3 373
  • 正文 我出身青樓苞笨,卻偏偏與公主長得像债朵,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子瀑凝,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,092評論 2 355

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