题目

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. sublime text 使用小技巧

    sublime下载各个版本 官网 插件官方网站地址 https://packagecontrol.io/ 一.安装设置字体及字体大小 1.点菜单“Preferences--->Setting - ...

  2. 关于if...else语句的小注意

    if...else是一个使用非常频繁的条件语句,在条件满足时执行if下的代码,条件不满足时执行else下的代码.但在使用过程中会由于粗心犯一些错误. 比如我想要把性别的默认值设置为“男”,应该进行如下 ...

  3. vim编辑Makefile如何使用Tab

    因为用vim编辑代码设置了Tab键为4个空格,但有时候我们需要编写Makefile,必须使用Tab,同时也不想设置set noexpandtab. 其实可以先Ctrl_v组合键,再按Tab键盘,这样我 ...

  4. 与C/C++关键字extern有关的原理

    关键字有一定的语义,但是用法不唯一. 对于C/C++语言的预编译.编译.汇编.链接.我相信大家在接触C++一年不到就背的滚瓜烂熟,但是其中的细节,是后来才慢慢想明白的.为什么我不讲extern关键字呢 ...

  5. Linux配置snmp

    机器环境 [root@linux-node1 ~]# cat /etc/redhat-release CentOS Linux release 7.1.1503 (Core) [root@linux- ...

  6. httpd基础知识

    apache简介   Apache取自"a patchy server"的读音,意思是充满补丁的服务器.Apache起初由伊利诺伊大学香槟分校的国家超级电脑应用中心(NCSA)开发 ...

  7. 量化分析v1

    量化分析v1 # -*- coding: utf-8 -*- """ Created on Wed Apr 11 10:13:32 2018 @author: chens ...

  8. 【idea】之使用SVN一些技巧

    @Copy https://www.cnblogs.com/whc321/p/5669804.html

  9. CSS之边框

    <!DOCTYPE html> <!--边框--> <html lang="en"> <head> <meta charset ...

  10. springBoot 整合mybaits 逆向工程

    pom.xml文件中增加配置项 <build> <plugins> <plugin> <groupId>org.springframework.boot ...