树的数值为[0, 9], 每一条从根到叶子的路径都构成一个整数,(根的数字为首位),求所有构成的所有整数的和

深度优先搜索,通过一个参数累加整数

  1. class Solution {
  2. public:
  3. void helper(TreeNode* node, int path, int& sum){
  4. if(!node){
  5. return;
  6. }
  7. //a(node)
  8. //lk("root",node)
  9. //a(path)
  10. //dsp
  11. if(!node->left && !node->right){
  12. sum+=path*+node->val;
  13. //dsp
  14. return;
  15. }
  16.  
  17. helper(node->left, path*+node->val, sum);
  18. helper(node->right, path*+node->val, sum);
  19. }
  20.  
  21. int sumNumbers(TreeNode* root) {
  22. int sum=;
  23. //ahd(root)
  24. //a(sum)
  25. helper(root, , sum);
  26. return sum;
  27. }
  28. };

程序运行动态演示:http://simpledsp.com/FS/Html/lc129.html

LeetCode 129. Sum Root to Leaf Numbers 动态演示的更多相关文章

  1. [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 ...

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

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

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

  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路径数字求和

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

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

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

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

随机推荐

  1. 第017讲:函数 - Python的乐高积木

    0. 你有听说过DRY吗? me:不知道 参考答案: 1. 都是重复一段代码,为什么我要使用函数(而不使用简单的拷贝黏贴)呢? me:函数可以设置参数. 参考答案:0) 可以降低代码量(调用函数只需要 ...

  2. SCUT - 365 - 鹏哥的数字集合 - wqs二分 - 斜率优化dp

    https://scut.online/p/365 https://www.luogu.org/problemnew/solution/P2365 写这篇的时候还不是很明白,看一下这个东西. http ...

  3. Ubuntu Anaconda3 环境下安装caffe

    安装Python环境 本人环境为Anaconda3 ,可参照 https://blog.csdn.net/ctwy291314/article/details/86571198 完成安装Python2 ...

  4. 行人重识别(ReID) ——基于MGN-pytorch进行可视化展示

    下载MGN-pytorch:https://github.com/seathiefwang/MGN-pytorch 下载Market1501数据集:http://www.liangzheng.org/ ...

  5. 基于 VirtualApp 结合 whale hook框架实现hook第三方应用

    要点 1. whale hook framework 使用示例: 2. 参考项目:VirtualHook: 3. 按照 VirtualHook 修改 VirtualApp: 4. 编写 hook pl ...

  6. Unity 官网无法访问|国外网站访问过慢|国外网站访问加速器

    目录 1. 文档地址 2. 按 3. 工具下载地址 1. 文档地址 GitHub博客 https://coco5666.github.io/blog/articles/20190704-01/ 2. ...

  7. MongoDB的应用

    一.MongoDB后台管理 # ./mongo MongoDB shell version v3.4.2 connecting to: mongodb://127.0.0.1:27017 MongoD ...

  8. less:避免编译

    .box { width: ~"calc(300px - 30px)"; } 编译成css .box { width: calc(300px - 30px); } importan ...

  9. ltp-ddt eth_switch_config学习

    # @name ALE Table test using SWITCH-CONFIG # @desc Checks default entries in ALE table and verifies ...

  10. jQuery入门、jQuery选择器、jQuery操作

    一.什么是jQuery及如何使用 1.1 jQuery 简介 jQuery是一个兼容多浏览器的javascript函数库(把我们常用的一些功能进行了封装,方便我们来调用,提高我们的开发效率.),核心理 ...