题目:

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,遍历二叉树,遇到叶子节点时,进行累加和,不多说,直接上代码。

实现代码:

#include <iostream>
#include <vector> using namespace std; /*
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. */ /**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/ struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}; void addNode(TreeNode* &root, int val)
{
if(root == NULL)
{
TreeNode *node = new TreeNode(val);
root = node;
}
else if(root->val < val)
{
addNode(root->right, val);
}
else if(root->val > val)
{
addNode(root->left, val);
}
} void printTree(TreeNode *root)
{
if(root)
{
cout<<root->val<<" ";
printTree(root->left);
printTree(root->right);
}
}
class Solution {
public:
int sumNumbers(TreeNode *root) {
if(root == NULL)
return 0;
int sum = 0;
vector<int> v;
dfs(root, v, sum);
return sum;
} void dfs(TreeNode *node, vector<int> &v, int &sum)
{
if(node == NULL)
return ; v.push_back(node->val);
if(node->left == NULL && node->right == NULL)
{
vector<int>::iterator iter;
int tmp = 0;
for(iter = v.begin(); iter != v.end(); ++iter)
tmp =tmp*10 + *iter;
sum += tmp; }
else
{
if(node->left)
dfs(node->left, v, sum);
if(node->right)
dfs(node->right, v, sum);
}
v.pop_back(); }
};
int main(void)
{
TreeNode *root = new TreeNode(5);
addNode(root, 7);
addNode(root, 3);
addNode(root, 9);
addNode(root, 1);
printTree(root);
cout<<endl; Solution solution;
int sum = solution.sumNumbers(root);
cout<<sum<<endl;
return 0;
}

LeetCode129:Sum Root to Leaf Numbers的更多相关文章

  1. LeetCode之“树”:Sum Root to Leaf Numbers

    题目链接 题目要求: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represe ...

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

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

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

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

  6. Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers)

    Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...

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

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

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

  9. [Swift]LeetCode129. 求根到叶子节点数字之和 | Sum Root to Leaf Numbers

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

随机推荐

  1. Ext JS 6 新特性和工具

    Ext JS 6 新特性和工具 Ext JS 6 带来很多新特性.工具和改进.以下是一些亮点: • 合并了 Ext JS & Sencha Touch - 在 Ext 6, 你可以访问 Ext ...

  2. Retrofit

    Retrofit 标签(空格分隔): 第三方&开源 Retrofit是一套RESTful架构的Android(Java)客户端实现,基于注解,提供JSON to POJO(Plain Ordi ...

  3. Jenkins Code Sign error: No provisioning profiles found

    === BUILD TARGET JenkinsTest OF PROJECT JenkinsTest WITH CONFIGURATION Release === Check dependencie ...

  4. C# 用代码创建 DataSet 和 DataTable 的列和记录

    System.Data.DataSet objSet = new DataSet(); System.Data.DataTable objTable = new DataTable("tes ...

  5. Rxlifecycle(一):使用

    Rxlifecycle使用非常方便简单,如下: 1.集成 build.gradle添加 //Rxlifecycle compile 'com.trello:rxlifecycle:0.3.1' com ...

  6. 使用SQL Server 2014 In-Memory 内存数据库时需要注意的地方

    转载: http://www.infoq.com/cn/articles/sql-server-2014-memory-database http://www.cnblogs.com/Amaranth ...

  7. [转]HTTP请求模型和头信息

    原文链接:http://www.java3z.com/cwbwebhome/article/article2/2406.html 目录 一.连接至Web服务器 二.发送HTTP请求 三.服务端接受请求 ...

  8. searchableselect不支持onchange的问题

    1.找到jquery.searchableSelect.js 2.找到selectItem函数 修改里面的方法,加入自定义你要回调的函数 selectItem: function(item){ //L ...

  9. iOS开发-NSDate使用

    时间戳是自 1970 年 1 月 1 日(00:00:00 GMT)至当前时间的总秒数.它也被称为 Unix 时间戳(Unix Timestamp). 下面是iOS中时间戳 与 时间之间的转换方法: ...

  10. log4j2配置

    在eclipse使用log4j2的时候遇到个问题: 我已经把log4j2.xml放到/src目录下了,而且设置从trace开始都打印到终端,但是我的程序里trace, info都不打印,到了error ...