File tree 1 file changed +32
-0
lines changed
1 file changed +32
-0
lines changed 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
+ * Tree | pre-order-traversal
11
+ * Time O(n) | Space O(n)
12
+ * https://leetcode.com/problems/sum-root-to-leaf-numbers
13
+ * @param {TreeNode } root
14
+ * @return {number }
15
+ */
16
+ var sumNumbers = function ( root ) {
17
+
18
+ let total = 0 ;
19
+ const dfs = ( node , num ) => {
20
+ if ( ! node . left && ! node . right ) {
21
+ num = num + node . val ;
22
+ total += + num ;
23
+ return ;
24
+ }
25
+
26
+ node . left && dfs ( node . left , num + node . val ) ;
27
+ node . right && dfs ( node . right , num + node . val ) ;
28
+ }
29
+
30
+ dfs ( root , "" ) ;
31
+ return total ;
32
+ } ;
You can’t perform that action at this time.
0 commit comments