- 浏览: 175859 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
计算所有路径的和,从根节点到叶子节点每条路径都代表一个十进制的数字。用递归从根节点开始,依次记录所有路径上组成数字的和。代码如下:
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) { if(root == null) return 0; return sum(root, root.val); } public int sum(TreeNode root, int value) { if(root == null) return value; if(root.left != null && root.right != null) return sum(root.left, value * 10 +root.left.val) + sum(root.right, value * 10 + root.right.val); if(root.left != null) return sum(root.left, value * 10 +root.left.val); if(root.right != null) return sum(root.right, value * 10 + root.right.val); return value; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 233Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 237You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 351Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 344Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 468Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 530Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 440Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 628Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 438The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 399Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 524Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 546Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 383All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 873Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 893Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 567Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 616Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 775Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 746You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 637For a undirected graph with tre ...
相关推荐
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
windows平台下的md5sum, 跟linux下的md5sum算出来的一模一样. 单文件, 直接在命令行下使用.
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)...
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.
Windows系统文件md5计算工具,md5sum命令。下载保存到C:\Windows或其他环境变量目录。