题目:

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. Hadoop - 任务调度系统比较

    1.概述 在Hadoop应用,随着业务指标的迭代,而使其日趋复杂化的时候,管理Hadoop的相关应用会变成一件头疼的事情,如:作业的依赖调度,任务的运行情况的监控,异常问题的排查等,这些问题会是的我们 ...

  2. 国内 Composer 镜像收集

    本文转载自: https://www.insp.top/article/composer-mirror-image 常见的: { "repositories": [ {" ...

  3. SAP GUI SAPLOGON.INI

    GUI是SAP系统最常用的客户端,在一台客户机上,利用GUI可以连接多套SAP系统(连接方法参见<客户端连接配置(SAP GUI 710)>),也可以设置多个快捷方式登录(参见<用快 ...

  4. Windows Phone 8.1 Update1 支持中文“小娜”及开发者模拟器更新

    千呼万唤的 Windows Phone 8.1 Update1 在 developer Perview 发布了还没有升级的朋友随我先睹为快吧.升级了的朋友们来看看 WP8.1 update1 还有哪些 ...

  5. Backbone1.0.0数据验证的变化

    0.5.3版本对Model数据验证时,绑定Error就可以了: (function(){ var Model = Backbone.Model.extend({ initialize : functi ...

  6. 【Android】Kill Service

    花了一天时间对如何Android保证Service不被杀死研究了一下,我的手机是Nexus5,系统4.4.2. 杀死一个Service通常有以下几种可能: 1)APP自己杀死(包括调用stopServ ...

  7. 一款基于HTML5的Web 3D开发工具

    在我们协助客户进行3D应用的开发过程中,客户遇到的最头疼的问题是如何在短时间内学会使用TWaver 3D引擎,以及使用TWaver 3D来创建和导入项目所需的各种3D业务模型.由于项目涵盖的行业繁多. ...

  8. NHibernate 使用CreateSQLQuery进行查询

    涉及的表:Cake{Id ,CakeName } CakeSize{ CakeId,-为外键,对应Cake表的字段Id Size } (其中ISession session = NHibernateH ...

  9. MyBatis知多少(22)MyBatis删除操作

    本节从表中使用MyBatis删除记录. 我们已经在MySQL下有EMPLOYEE表: CREATE TABLE EMPLOYEE ( id INT NOT NULL auto_increment, f ...

  10. Linux 时钟与计时器

    对 Linux 系统来说,时钟和计时器是两个十分重要的概念.时钟反应的是绝对时间,也可认为是实时时间.计时器反应的则是相对时间,即相对于系统启动后的计时.操作系统内核需要管理运行时间(uptime)和 ...