File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change 68
68
| 119| [ 杨辉三角 II] ( https://leetcode.cn/problems/pascals-triangle-ii/ ) | [ JavaScript] ( ./algorithms/pascals-triangle-ii.js ) | Easy|
69
69
| 124| [ 二叉树中的最大路径和] ( https://leetcode.cn/problems/binary-tree-maximum-path-sum/ ) | [ JavaScript] ( ./algorithms/binary-tree-maximum-path-sum.js ) | Hard|
70
70
| 125| [ 验证回文串] ( https://leetcode.cn/problems/valid-palindrome/ ) | [ JavaScript] ( ./algorithms/valid-palindrome.js ) | Easy|
71
+ | 129| [ 求根节点到叶节点数字之和] ( https://leetcode.cn/problems/sum-root-to-leaf-numbers/ ) | [ JavaScript] ( ./algorithms/sum-root-to-leaf-numbers.js ) | Medium|
71
72
| 136| [ 只出现一次的数字] ( https://leetcode-cn.com/problems/single-number/ ) | [ JavaScript] ( ./algorithms/single-number.js ) | Easy|
72
73
| 141| [ 环形链表] ( https://leetcode-cn.com/problems/linked-list-cycle/ ) | [ JavaScript] ( ./algorithms/linked-list-cycle.js ) | Easy|
73
74
| 142| [ 环形链表 II] ( https://leetcode.cn/problems/linked-list-cycle-ii/ ) | [ JavaScript] ( ./algorithms/linked-list-cycle-ii.js ) | Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * function TreeNode(val, left, right) {
4
+ * this.val = (val===undefined ? 0 : val)
5
+ * this.left = (left===undefined ? null : left)
6
+ * this.right = (right===undefined ? null : right)
7
+ * }
8
+ */
9
+ /**
10
+ * 129. 求根节点到叶节点数字之和
11
+ * @param {TreeNode } root
12
+ * @return {number }
13
+ */
14
+ var sumNumbers = function ( root ) {
15
+ let sum = 0 ;
16
+ const dfs = ( root , val = 0 ) => {
17
+ if ( root ) {
18
+ val = val * 10 + root . val ;
19
+ if ( ! root . left && ! root . right ) {
20
+ sum += val ;
21
+ }
22
+ dfs ( root . left , val ) ;
23
+ dfs ( root . right , val ) ;
24
+ }
25
+ } ;
26
+ dfs ( root ) ;
27
+
28
+ return sum ;
29
+ } ;
You can’t perform that action at this time.
0 commit comments