题目:

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. 使用IntelliJ IDEA 14和Maven创建java web项目

    参考地址 http://www.cnblogs.com/jifeng/p/4658765.html

  2. css3 定义选择器

    引言:CSS样式规则有两个主要部分.选择器决定将格式化应用到哪些元素.声明则定义要应用的格式化. 构造选择器 选择器可以定义五个不同的标准来选择要进行格式化的元素. 元素的类型或者名称.如下所示. h ...

  3. HTML单选按钮样式更换

    <!DOCTYPE html > <html lang="en"> <head> <meta http-equiv="Conte ...

  4. UNIX环境高级编程笔记之进程控制

    本章重点介绍了进程控制的几个函数:fork.exec族._exit.wait和waitpid等,主要需要掌握的是父进程和子进程之间的运行机制,怎么处理进程的正常和异常终止.以及怎么让进程执行不同的程序 ...

  5. ux.form.field.Verify 验证码控件

    //验证码控件 Ext.define('ux.form.field.Verify', { extend: 'Ext.container.Container', alias: ['widget.fiel ...

  6. JIRA数据库切换(HSQL Database到MySQL)

    没试过的版本,但是看着靠谱: 注:如果你之前配置过hsql版本,这次修改mysql数据操作会导致配置数据丢失,所以请注意备份 一. 创建名为jiradb的mysql数据,编码格式为utf-8 二.修改 ...

  7. Part 1: Running Oracle E-Business Suite on Oracle Cloud

    You can now run Oracle E-Business Suite on Oracle Cloud.  EBS customers can take advantage of rapid ...

  8. Eclipse 常用最新插件.标记

    Properties Editor     编辑java的属性文件,并可以自动存盘为Unicode格式 http://marketplace.eclipse.org/content/propertie ...

  9. Log4net对数据库的支持

    记录到Oracle数据库中 <appender name="AdoNetAppender_Oracle" type="log4net.Appender.AdoNet ...

  10. zk框架销毁Page上的Component

    销毁Page上的Component ZK的组件之间是树状结构的,每一组件都只有一个根. 从页面上销毁一个组件可以通过下面两种方式来实现: 1. 组件不是根组件时:Component.setParent ...