DFS的标准形式

用一个String记录路径,最后判断到叶子时加到结果上。

int res = 0;public int sumNumbers(TreeNode root) {if (root==null)return res;helper(root,"");return res;}public void helper(TreeNode root,String s){s += root.val+"";if (root.left==null&&root.right==null){res+=Integer.parseInt(s);return;}if (root.left!=null) helper(root.left,s);if (root.right!=null) helper(root.right,s);}

转载于:https://www.cnblogs.com/stAr-1/p/8358036.html

[LeetCode]129. Sum Root to Leaf Numbers路径数字求和相关推荐

  1. 【同113】LeetCode 129. Sum Root to Leaf Numbers

    LeetCode 129. Sum Root to Leaf Numbers Solution1:我的答案 二叉树路径和问题,类似113 /*** Definition for a binary tr ...

  2. 129. Sum Root to Leaf Numbers

    输入: [1,2,3]1/ \2 3 输出: 25 解释: 从根到叶子节点路径 代表数字 . 从根到叶子节点路径 代表数字 . 因此,数字总和 = 12 + 13 = 25 class Solutio ...

  3. 【LeetCode】Sum Root to Leaf Numbers

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

  4. 【LeetCode从零单排】No129 Sum Root to Leaf Numbers

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

  5. LeetCode Sum Root to Leaf Numbers (dfs)

    问题:给出一个二叉树,求从根结点到叶子结点路径上构成的所有数的和 思路:自顶向下遍历过程中,递归的终止条件是,如果是叶子结点,返回上一层的数*10加上叶子结点的值.在遍历过程中,如果存在左子树,计算左 ...

  6. 【LeetCode-面试算法经典-Java实现】【129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)】...

    [129-Sum Root to Leaf Numbers(全部根到叶子结点组组成的数字相加)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bina ...

  7. Leetcode 129求根节点到叶节点数字之和、104二叉树的最大深度、8字符串转换整数(atoi)、82删除排序链表中的重复元素II、204二分查找、94二叉树的中序遍历、144二叉树的前序遍历

    Top1:Leetcode 129求根节点到叶节点数字之和 官方题解:https://leetcode.cn/problems/sum-root-to-leaf-numbers/solution/qi ...

  8. LeetCode--Sum Root to Leaf Numbers

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

  9. LeetCode Algorithm 129. 求根节点到叶节点数字之和

    129. 求根节点到叶节点数字之和 Ideas 从根节点到叶节点的路径可以通过递归得到,所以我们从根节点开始递归,每次递归的时候还带着一个参数表示从根节点到当前节点所组成的数字对应的字符串,最后如果到 ...

  10. LeetCode 421. Maximum XOR of Two Numbers in an Array--Python解法

    LeetCode 421. Maximum XOR of Two Numbers in an Array–C++,Python解法 LeetCode题解专栏:LeetCode题解 我做的所有的Leet ...

最新文章

  1. HashMap 的 7 种遍历方式与性能分析!(强烈推荐)
  2. boost::mp11::mp_similar相关用法的测试程序
  3. Hadoop集群环境下网络架构的设计与优化
  4. 蓝桥杯2016年七届C/C++省赛C组第三题-平方怪圈
  5. 【Visual Assist X】VAssistX的安装和使用
  6. java jshell_[Java JShell 指南] - 介绍
  7. [复变函数]第20堂课 5.4 整函数与亚纯函数的概念
  8. GPA计算 UESTC - 152
  9. 计算机表格用计算公式百分百,excel表格怎么算数据的阳性比率-计算机一级用excel表格计算增长比例该怎么算?...
  10. 自媒体剪辑视频都在用的6个音效素材网站。
  11. O_APPEND的含义
  12. Python实现词云生日祝福图片,快拿去给心爱的人送上祝福
  13. 【单例深思】枚举实现单例原理
  14. 着手社区建设掌握的两个概念
  15. 助力“双创”提质提速,柳枝行动再出发
  16. 基础篇:3.1.4)注塑件-热熔
  17. socket 配置选项 理解 socket errorno 情况收集
  18. ESP32 开发笔记(三)源码示例 6_TouchPad_Interrupt 电容触摸中断实现触摸按钮
  19. python银行自动取款机系统详写
  20. 使用barrier将window和ubuntu共用键鼠的方法

热门文章

  1. 西班牙语dele等级_2020年西班牙语DELE考试时间一览表
  2. Python中 sys.argv[]
  3. Mac 系统下 配置多个ssh-key (git sourcetree)
  4. golang 编译 执行时候报错cannot declare name db.Query
  5. 2.8.PHP7.1 狐教程-【控制语句 Switch】
  6. Spring Boot 文件上传下载、url 映射虚拟路径、解决下载文件名称乱码
  7. JS 数组 Array 对象详解 与 for...of 增强型迭代
  8. 小马哥-Java 微服务实践 - Spring Boot 系列-01Java 微服务实践 - Spring Boot 系列(一)初体验...
  9. 阶段3 1.Mybatis_10.JNDI扩展知识_1 补充-JNDI概述和原理
  10. Spring 全局异常拦截根据业务返回不同格式数据 自定义异常