[Leetcode] Sum Root to Leaf Numbers (Java)
来源:互联网 发布:inpho软件好学吗 编辑:程序博客网 时间:2024/05/16 08:56
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
public class Solution { public int sumNumbers(TreeNode root) { if(root==null)return 0;int ret=0;ArrayList<Integer> tmp = new ArrayList<Integer>();dfs(tmp,root,"");for(int i:tmp){ret+=i;}return ret;}private void dfs(ArrayList<Integer> tmp, TreeNode root,String string) { string+=root.val;if(root.left==null&&root.right==null){tmp.add(Integer.parseInt(string));return;}if(root.left!=null)dfs(tmp, root.left, string);if(root.right!=null)dfs(tmp, root.right, string);}}
0 0
- [Leetcode] Sum Root to Leaf Numbers (Java)
- [LeetCode][Java] Sum Root to Leaf Numbers
- leetcode:Sum Root to Leaf Numbers 【Java】
- leetcode-java-129. Sum Root to Leaf Numbers
- 【leetcode】129. Sum Root to Leaf Numbers【java】
- [leetcode]129. Sum Root to Leaf Numbers@Java解题报告
- leetcode 86: Sum Root to Leaf Numbers
- [LeetCode] Sum root to leaf numbers
- LeetCode : Sum Root to Leaf Numbers
- [leetcode] Sum Root to Leaf Numbers
- 【leetcode】Sum Root to Leaf Numbers
- LeetCode之sum root to leaf numbers
- leetcode:Sum Root to Leaf Numbers
- [LeetCode]Sum Root to Leaf Numbers
- [Leetcode]Sum Root to Leaf Numbers
- [leetcode]Sum Root to Leaf Numbers
- leetcode Sum Root to Leaf Numbers
- LeetCode-Sum Root to Leaf Numbers
- Winfrom和控制台中static修饰方法的问题
- Hash思想:映射
- 《基于ORACLE SQL优化》读书笔记-使用SQL PROFILE锁定执行计划
- listview onListItemClick 失效解决方案
- IOS点击return隐藏键盘
- [Leetcode] Sum Root to Leaf Numbers (Java)
- 将man page转换成pdf的方法
- HTTP 方法:GET 对比 POST
- 让/etc/profile文件修改后立即生效
- TOMCAT调优(JVM参数)
- 文分词 mmseg4j
- Android 横竖屏切换时 Custom Dialog 布局自适应
- BOX2D基本元素简介
- Java内存溢出