- 浏览: 183552 次
- 性别:
- 来自: 济南
文章分类
最新评论
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 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 673Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For 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 ...
python python_leetcode题解之129_Sum_Root_to_Leaf_Numbers
javascript js_leetcode题解之129-sum-root-to-leaf-numbers.js
在本压缩包中,我们关注的是一个与Python编程和LeetCode面试相关的题目,具体是第129题——"Sum Root to Leaf Numbers"(根到叶节点数字之和)。这是一道典型的树形结构问题,涉及到二叉树的遍历。在LeetCode上,...
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**:计算二叉树所有从根到叶子节点的路径之和。 4. **动态规划(Dynamic Programming)**: - **Best Time To Buy and Sell Stock**:股票交易的最佳时机问题。 - **Unique Paths**...
二叉搜索树迭代器-Sum Root to Leaf Numbers 广度优先搜索 -二叉树级顺序遍历-二叉树级顺序遍历II - 二叉树之字形层序遍历-课程安排-课程表二-岛屿数量-太平洋大西洋水流-周边地区-对称树- 克隆图- 字梯-字梯II 回溯...
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
lru cache leetcode leetcode 记录自己刷leetcode时遇到的一些值得记下来的题目, 分为一些子项 bytedance daily:每日一题 struct-algorithm:数据结构/算法先关 ...sum-root-to-leaf-numbers best-time-to-buy
gas station leetcode 在牛客网上的在线编程中的leetcode在线编程题解 代码中有详细题解 ...sum-root-to-leaf-numbers 动态规划 distinct-subsequences 递归 valid-palindrome 模拟 pascals-triangle 模拟 pasca
Sum_Root_to_Leaf_Numbers DFS Surrounded_Regions 由四周‘O’开始向内检索,利用第三个字符对可以变换的‘O’进行暂存 Word_Lader BFS,避免DFS记录以遍历的节点错误,且保证优先找到最短的路径, 按照图的方法判断...