题目如下:

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,随时记录当前数字,当前和,返回即可

代码如下:

/**

 * Definition for binary tree

 * struct TreeNode {

 *     int val;

 *     TreeNode *left;

 *     TreeNode *right;

 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}

 * };

 */

class Solution {

public:

    int sumNumbers(TreeNode *root) {

        // Start typing your C/C++ solution below

        // DO NOT write int main() function

        

        int sum = 0;

        int curNumber = 0;

        

        sumNumbers_my(root,curNumber,sum);

        

        return sum;

        

    }

    void sumNumbers_my(TreeNode *root,int &curNumber,int &sum)

    {

        if(root == NULL)return;

        

        curNumber = curNumber*10+root->val;

        int curNumber_bk = curNumber;

        

        if(root ->left== NULL&&root->right ==NULL)

        {

            sum+= curNumber;return;

        }

        

        sumNumbers_my(root->left,curNumber,sum);

        curNumber = curNumber_bk;

        sumNumbers_my(root->right,curNumber,sum);

        return;

    }

};

leetcode—sum root to leaf number的更多相关文章

  1. LeetCode: Sum Root to Leaf Numbers 解题报告

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

  2. [leetcode]Sum Root to Leaf Numbers @ Python

    原题地址:http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/ 题意: Given a binary tree containing di ...

  3. [LeetCode] 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 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: Sum Root to Leaf Numbers [129]

    [题目] Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a n ...

  6. [Leetcode] Sum root to leaf numbers求根到叶节点的数字之和

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

  7. LeetCode -- Sum Root to Leaf NNumbers

    Related Links: Path Sum: http://www.cnblogs.com/little-YTMM/p/4529982.html Path Sum II: http://www.c ...

  8. LeetCode :: Sum Root to Leaf Numbers [tree、dfs]

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

  9. [LeetCode] Sum Root to Leaf Numbers dfs,深度搜索

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

随机推荐

  1. 软件调试之INT 3讲解

    第4章断点和单步执行 断点和单步执行是两个经常使用的调试功能,也是调试器的核心功能.本章我们将介绍IA-32 CPU是如何支持断点和单步执行功能的.前两节将分别介绍软件断点和硬件断点,第4.3节介绍用 ...

  2. 【leetcode】Palindrome Number (easy)

    Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...

  3. 探讨read的返回值的三种情况

    http://blog.chinaunix.net/uid-23629988-id-3035613.html 今天探讨一个很看似简单的API “read”的返回值问题.read的返回值有哪几个值?每个 ...

  4. 选择排序的openMP实现

    // test.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <stdio.h> #include < ...

  5. Trainning Guide的代码

    今天无意间找到了训练指南的网上代码,都是刘汝佳写的,在这. 今天在做这题1400 - "Ray, Pass me the dishes!",我写的线段树的思路跟上次的Frequen ...

  6. Django Navi 重用

    代码来自这里: base.html <html> <head>...</head> <body> ... {% block nav %} <ul ...

  7. CentOS7 64位 自动分配IP地址设置

    vim /etc/sysconfig/network-scripts/ifcfg-eno16777736 # ifcfg-后接网卡名称 配置如下,ONBOOT设置为yes HWADDR=:0C::E9 ...

  8. MapReduce编程系列 — 2:计算平均分

    1.项目名称: 2.程序代码: package com.averagescorecount; import java.io.IOException; import java.util.Iterator ...

  9. node.js模块之Buffer模块

    http://nodejs.org/api/buffer.html Pure JavaScript is Unicode friendly but not nice to binary data. W ...

  10. [译]Atomic VS. Non-Atomic 操作

    原文链接:atomic-vs-non-atomic-operations 在网上已经写了很多关于原子操作的文章,但是通常都集中在原子的读-修改-写(RMW. read-modify-write)操作. ...