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.

问题:给定一个二叉树,树的每个节点都只包含一个数字 0-9,将根节点到叶子节点路径上的元素值组合成一个整数,求所有整数和。

这是一道深度遍历的应用。深度遍历二叉树一般是递归遍历,或者借助栈遍历。每到达一个叶子节点,都需要重新遍历一次根节点到该叶子节点路径的值,借组栈遍历可以实现满足这个需要。

     #define null_symble  -2147483648

     int sumNumbers(TreeNode* root) {

         if(root == NULL){
return ;
} list<TreeNode*> stackt;
stackt.push_back(root); int sum = ; map<TreeNode*, int> node_val; while(stackt.size() > ){
TreeNode* node = stackt.back(); if(node->val != null_symble){
node_val[node] = node->val;
node->val = null_symble;
} if (node->left != NULL && node->left->val != null_symble){
stackt.push_back(node->left);
continue;
} if(node->right != NULL && node->right->val != null_symble){
stackt.push_back(node->right);
continue;
} if(node->left == NULL && node->right == NULL){
string str = "";
list<TreeNode*>::iterator l_iter;
for(l_iter = stackt.begin(); l_iter != stackt.end(); l_iter++){
str += to_string(node_val[*l_iter]);
}
sum += stoi(str);
}
stackt.pop_back();
} return sum;
}

[LeetCode] 129. Sum Root to Leaf Numbers 解题思路的更多相关文章

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

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

  2. [LeetCode] 129. 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. leetcode@ [129] Sum Root to Leaf Numbers (DFS)

    https://leetcode.com/problems/sum-root-to-leaf-numbers/ Given a binary tree containing digits from 0 ...

  4. Java for LeetCode 129 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 129. Sum Root to Leaf Numbers ----- java

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

  6. Leetcode#129 Sum Root to Leaf Numbers

    原题地址 二叉树的遍历 代码: vector<int> path; int sumNumbers(TreeNode *root) { if (!root) ; ; path.push_ba ...

  7. LeetCode 129. Sum Root to Leaf Numbers 动态演示

    树的数值为[0, 9], 每一条从根到叶子的路径都构成一个整数,(根的数字为首位),求所有构成的所有整数的和 深度优先搜索,通过一个参数累加整数 class Solution { public: vo ...

  8. [LeetCode]129. Sum Root to Leaf Numbers路径数字求和

    DFS的标准形式 用一个String记录路径,最后判断到叶子时加到结果上. int res = 0; public int sumNumbers(TreeNode root) { if (root== ...

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

随机推荐

  1. Java 编程的动态性,第 6 部分: 利用 Javassist 进行面向方面的更改--转载

    本系列的 第 4 部分和 第 5 部分讨论了如何用 Javassist 对二进制类进行局部更改.这次您将学习以一种更强大的方式使用该框架,从而充分利用 Javassist 对在字节码中查找所有特定方法 ...

  2. wildcard

    [rusky@rhel7 test]$ lstest1  test123  test2  test317  test33  test335  test336  test44  testtest[rus ...

  3. NS2仿真:公交车移动周期模型及性能分析

    NS2仿真实验报告3 实验名称:公交车移动周期模型及性能分析 实验日期:2015年3月16日~2015年3月21日 实验报告日期:2015年3月22日 一.实验环境(网络平台,操作系统,网络拓扑图) ...

  4. 计算方法(一)用C#实现数值迭代

    平时,经常会遇到解方程,计算方法中常用的有二分法(精度太低,迭代次数多,一般没人用),牛顿迭代法,弦截法,网上大多都是C++或者Java的实现代码,很少有C#的,我在本科毕业论文中用到了这些,那时也需 ...

  5. Linux LVM 扩展磁盘分区

    系统:centos 6.3--新建分区 fdisk -l /dev/sdc       # 查看分区 fdisk /dev/sdc          # 创建分区 :n                 ...

  6. Html.RenderPartial和Html.RenderAction的区别

    添加一个PartialController控制器 using System; using System.Collections.Generic; using System.Linq; using Sy ...

  7. img图片元素下多余空白解决方案

    在进行页面的DIV+CSS排版时,遇到IE6(当然有时Firefox下也会偶遇)浏览器中的图片元素img下出现多余空白的问题绝对是常见的对於 该问题的解决方法也是「见机行事」,根据原因的不同要用不同的 ...

  8. linux查询当前进程数的命令

    $command = "ps aux | grep  'zb_insure/get_order_info_from_queue.php' | grep -v 'grep' |  grep - ...

  9. MySQL数据库my.cnf配置文件注释详解

    我们知道,在MySQL数据库安装完成后,要对my.cnf配置文件进行适当的修改才能充分利用MySQL数据库的功能.但是对于初学者来说,修改my.cnf配置文件似乎是一个比较难的过程.为了解决这个问题, ...

  10. Java直接插入算法

    直接插入算法是将N个带排序的元素看做成一个有序表和一个无序表. 每次从无序表中取一个元素和有序表比较,重复N-1次完成排序. 直接上代码: package test; public class Tes ...