LeetCode:129. Sum Root to Leaf Numbers

286人阅读 评论(0) 收藏 举报
分类:

LeetCode: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.

题目大意: 将二叉树的每一条 root-to-leaf 路径看做一个十进制数字,计算所有的路径构成数字的和。

解题思路 —— 深度搜索

深度搜索,在每个叶节点计算到达当前节点路径的数字,并将其加入和中。

AC 代码

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
    // 获取 root 的 “Sum Root to Leaf Numbers”, num表示当前root-to-curNode的数字
    int sumNumbersRef(TreeNode* root, int num = 0)
    {
        if(root == nullptr) return 0;

        num = num*10 + root->val;

        if(root->left == nullptr && root->right == nullptr) return num;
        else return sumNumbersRef(root->left, num) + sumNumbersRef(root->right, num);
    }
public:
    int sumNumbers(TreeNode* root) {
        return sumNumbersRef(root);
    }
};
查看评论

LeetCode(129)SumRoottoLeafNumbers

题目如下: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent ...
  • feliciafay
  • feliciafay
  • 2014-02-28 15:57:49
  • 1344

【LeetCode-面试算法经典-Java实现】【129-Sum Root to Leaf Numbers(所有根到叶子结点组组成的数字相加)】

【129-Sum Root to Leaf Numbers(所有根到叶子结点组组成的数字相加)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given a binary...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-15 06:11:18
  • 1916

Sum Root to Leaf Numbers -- LeetCode

原题链接: http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/  这是一道树的题目,一般使用递归来做,主要就是考虑递归条件和结束条件。...
  • linhuanmars
  • linhuanmars
  • 2014-04-04 09:20:44
  • 8906

LeetCode 129:Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a numb...
  • geekmanong
  • geekmanong
  • 2015-12-31 14:30:09
  • 1537

leetcode Sum Root to Leaf Numbers 详细思考分析过程

做这道题的时候,我自己遇到的问题: 1. 如何分情况? 2. 如何递归到该点的时候该如何操作? 3. 由跟节点到叶子节点的时候,每进入一层就需要把前面的值*10,如何处理这个值?总值又是如何保存的...
  • kenden23
  • kenden23
  • 2013-11-03 09:15:06
  • 8922

【LeetCode】Sum Root to Leaf Numbers 解题报告

【题目】 Given a binary tree containing digits from 0-9 only, each root-to-leaf path could repres...
  • ljiabin
  • ljiabin
  • 2014-09-22 17:14:49
  • 2968

129. Sum Root to Leaf Numbers**

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a numbe...
  • alwaystry
  • alwaystry
  • 2016-12-24 15:13:04
  • 132

【leetcode】129. Sum Root to Leaf Numbers【java】

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a numb...
  • cslbupt
  • cslbupt
  • 2017-01-16 11:16:53
  • 121

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...
  • wusecaiyun
  • wusecaiyun
  • 2015-08-14 21:06:34
  • 224

Leetcode 129. Sum Root to Leaf Numbers

QuestionGiven a binary tree containing digits from 0-9 only, each root-to-leaf path could represent ...
  • zhangwj0101
  • zhangwj0101
  • 2016-03-24 12:23:13
  • 111
    个人资料
    持之以恒
    等级:
    访问量: 35万+
    积分: 2940
    排名: 1万+
    友情链接
    最新评论