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 a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public int sumNumbers(TreeNode root) { return solve(root, 0); } private int solve(TreeNode root, int level) { if (root == null) { return 0; } if (root.left==null && root.right==null) { return level + root.val; } int nextLevel = (level + root.val) * 10; int left = solve(root.left, nextLevel); int right = solve(root.right, nextLevel); return left + right; } }
相关推荐
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. An example is the root-to-leaf path 1->2->3 which represents ...
北大POJ2739-Sum of Consecutive Prime Numbers 解题报告+AC代码
Sum Root to Leaf Numbers LCA of Binary Tree 线段树 Range Sum Query - Mutable 排序 插入排序 Insertion Sort List 归并排序 Merge Two Sorted Arrays Merge Two Sorted Lists Merge k Sorted Lists Sort List ...
numbers 2020-01-22 226 翻转二叉树 2020-01-23 95 不同的二叉搜索树 -变种 96 before 2020-01-24 110 平衡二叉树 -结束之后完成 1227 飞机座位分配概率 2020-01-27 208 字典树 2020-01-28 116 Populating Next ...
二叉搜索树迭代器-Sum Root to Leaf Numbers 广度优先搜索 -二叉树级顺序遍历-二叉树级顺序遍历II - 二叉树之字形层序遍历-课程安排-课程表二-岛屿数量-太平洋大西洋水流-周边地区-对称树- 克隆图- 字梯-字梯II 回溯...
到达角估计,DOA estimation,MUSIC algorithm, 'Delay and Sum algorithm', 'Capon algorithm',' 'MUSIC+spatial smoothing', ... 'Root MUSIC algorithm',' ','ESPRIT algorithm'
自然数方幂和的又一种计算方法,朱玉扬,段宝彬, 将排列数表成一个自然数的各个不同指数的线性组合,求其相应的系数矩阵及其逆矩阵,指出其系数矩阵为下三角的,逆矩阵为上三角�
Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t=4, n=6, and the list is [4,3,2,2,1,1], then there are four ...
While-Loop-Sum-User-Numbers-Activity-7 原始课程中的活动6
leetcode 530 LeetCode 问题列表,包括锁定的问题。 [1028] ...Numbers Easy (45.80 %) [1021] Remove Outermost Parentheses Easy (80.59 %) [1020] Number of Enclaves Medium (54.07 %) [1019] Next G
a program to sum array elements
用汇编语言编写从键盘输入N,代表N个数,就会从1一直加到N求和
Blue Prism 实例练习文件。 该文件用于示范如何运行一段Process,以及演示Data Item
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2)...
windows平台下的md5sum, 跟linux下的md5sum算出来的一模一样. 单文件, 直接在命令行下使用.
LINQ to SQL语句(3)之Count/Sum/Min/Max/Avg LINQ to SQL语句(4)之Join LINQ to SQL语句(5)之Order By LINQ to SQL语句(6)之Group By/Having LINQ to SQL语句(7)之Exists/In/Any/All/Contains LINQ to SQL语句(8)之...
介绍ldpc的sum-product算法的ppt文件。
md5sum sha1sum for windows 微软官方md5sum和sha1sum工具,校验速度很快。
Write Java program called AverageNumbers.java to determine the average of several numbers.
这个网站上很多md5的源代码都是有问题的,我就重提交一个跟md5sum运行效果一样的源码供大家参考