题目:

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. atom 安装插件出现 EIO 错误

    今天给 atom 安装一个插件autocomplete-python的时候出现错误 npm ERR! Windows_NT 6.1.7600 npm ERR! argv "C:\\Progr ...

  2. Rxlifecycle(二):源码解析

    1.结构 Rxlifecycle代码很少,也很好理解,来看核心类. 接口ActivityLifecycleProvider RxFragmentActivity.RxAppCompatActivity ...

  3. Eclipse+Selenium自动化测试脚本设计V1.0

    Eclipse+Selenium自动化测试脚本设计V1.0 http://www.docin.com/p-803032251.html

  4. Maximum Entropy Markov Models for Information Extraction and Segmentation

    1.The use of state-observation transition functions rather than the separate transition and observat ...

  5. mono+jexus 之连接数据库

    System.ArgumentException Unable to find the requested .Net Framework Data Provider. It may not be in ...

  6. Til the Cows Come Home(最短路)

    Til the Cows Come Home Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I ...

  7. c/c++:动态库 静态库 linux/windows 例子 (转)

    作者:吴秦出处:http://www.cnblogs.com/skynet/本文基于署名 2.5 中国大陆许可协议发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名吴秦(包含链接). C++静 ...

  8. SNF开发平台WinForm之三-开发-单表选择控件创建-SNF快速开发平台3.3-Spring.Net.Framework

    3.1运行效果: 3.2开发实现: 3.2.1 这个开发与第一个开发操作步骤是一致的,不同之处就是在生成完代码之后,留下如下圈红程序,其它删除. 第一个开发地址:开发-单表表格编辑管理页面 http: ...

  9. (转)linux内核虚拟文件系统浅析

    转自http://hi.baidu.com/_kouu/item/4e9db87580328244ef1e53d0 ###### 虚拟文件系统(VFS)在我看来, "虚拟"二字主要 ...

  10. css3,background-clip/background-origin的使用场景,通俗讲解

    先不说background-clip/background-origin的用法,我们先来聊聊css背景方面的知识. <!DOCTYPE html> <html lang=" ...