题目

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.

解答

今天又选了一道水题,就是二叉树的先序遍历,又是一遍AC。。。感觉这种题目都不用动脑子,明天选一道要动脑子的题目。

下面是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 {
public:
    int sum = 0;
    int temp = 0;
    void preorder(TreeNode *node){
        if(node == NULL){
            return ;
        }
        temp = temp * 10 + node->val;
        if(node->left == NULL && node->right == NULL){
            sum += temp;
        }
        else{
            preorder(node->left);
            preorder(node->right);
        }
        temp = (temp - node->val) / 10;
        return ;
    }
    int sumNumbers(TreeNode* root) {
        preorder(root);
        return sum;
    }
};

138

LeetCode OJ 129. Sum Root to Leaf Numbers的更多相关文章

  1. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  2. 【LeetCode】129. Sum Root to Leaf Numbers (2 solutions)

    Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf path ...

  3. 【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 ...

  4. LeetCode OJ:Sum Root to Leaf Numbers(根到叶节点数字之和)

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

  5. 【LeetCode OJ】Sum Root to Leaf Numbers

    # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val = x # self ...

  6. LeetCode解题报告—— Sum Root to Leaf Numbers & Surrounded Regions & Single Number II

    1. Sum Root to Leaf Numbers Given a binary tree containing digits from 0-9 only, each root-to-leaf p ...

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

  8. leetcode@ [129] Sum Root to Leaf Numbers (DFS)

    https://leetcode.com/problems/sum-root-to-leaf-numbers/ Given a binary tree containing digits from 0 ...

  9. 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 number ...

随机推荐

  1. ArcGIS 按多边形区域统计栅格影像的一些信息

    在使用ArcGIS对栅格影像进行分析时,难免要进行一些统计类的分析.如统计框选区域的像素的个数,面积.均值等内容. 下面给出使用“Spatial Analyst Tools -- > Zonal ...

  2. SpringMVC @RequestBody @RequestParam @PathVariable 等参数绑定注解详解

    request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用: http://blog.csdn.net/walkerjong/article/details/794 ...

  3. 关于Jordan标准形

    [转载请注明出处]http://www.cnblogs.com/mashiqi 2017/06/25 设$A$是$n$维线性空间$V$上的线性变换,它的特征值与相应的代数重数分别为$\lambda_i ...

  4. Cortex-M3 跳转到指定bin执行

    跳转前指定sp和msp: #if defined(__GNUC__) __attribute__(( naked )) static void set_sp(unsigned long addr) { ...

  5. 软件工程第4次作业------石墨文档Android客户端案例分析

    作业要求的博客链接:https://edu.cnblogs.com/campus/nenu/2016CS/homework/2505 分析产品:石墨文档Android客户端 第一部分 调研和评测 1. ...

  6. python的高阶函数式编程

    首先   函数式编程≠函数编程,就跟计算机≠计算,因为计算机基于硬件,计算基于算法,所以函数式编程是倾向于算法. 高阶函数定义: 一个函数接受的这个参数,而这个参数也是一个函数,称之为高阶函数 例如: ...

  7. zombodb安装试用

    pg 数据库安装 参考如下安装 yum install https://download.postgresql.org/pub/repos/yum/10/redhat/rhel-7-x86_64/pg ...

  8. java_basic_基础

    变量 类型 运算符 条件 循环 调试 字符串 数组 从键盘输入数据 switch的用法 从变量接收值 从键盘接收值 输出到一个文件 基本类型的赋值与引用类型的赋值是不一样的 是将引用类型的地址 每一个 ...

  9. 【转载】robocopy的用法

    经常进行文件管理操作的朋友们,不满意于Windows系统内置的复制功能,因为它太龟速了.于是大家就使用FastCopy.TeraCopy之类的软件来加速复制,但是你是否知道Windows 7已经内置快 ...

  10. html动态元素点击事件添加

    很多时候,页面的元素是后期异步动态添加在页面上.页面点击事件无效. 非动态的元素直接$().click();便可以直接触发点击事件,而动态元素需要事先注册事件. $(document).on('cli ...