Skip to content

Commit f089b4f

Browse files
committed
solve problem Sum Root To Leaf Numbers
1 parent ceeb217 commit f089b4f

File tree

5 files changed

+67
-0
lines changed

5 files changed

+67
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ All solutions will be accepted!
283283
|200|[Number Of Islands](https://leetcode-cn.com/problems/number-of-islands/description/)|[java/py/js](./algorithms/NumberOfIslands)|Medium|
284284
|223|[Rectangle Area](https://leetcode-cn.com/problems/rectangle-area/description/)|[java/py/js](./algorithms/RectangleArea)|Medium|
285285
|419|[Battleships In A Board](https://leetcode-cn.com/problems/battleships-in-a-board/description/)|[java/py/js](./algorithms/BattleshipsInABoard)|Medium|
286+
|129|[Sum Root To Leaf Numbers](https://leetcode-cn.com/problems/sum-root-to-leaf-numbers/description/)|[java/py/js](./algorithms/SumRootToLeafNumbers)|Medium|
286287

287288
# Database
288289
|#|Title|Solution|Difficulty|
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Sum Root To Leaf Numbers
2+
This problem is easy to solve by DFS
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public int sumNumbers(TreeNode root) {
12+
return root == null ? 0 : helper(root, 0);
13+
}
14+
15+
public int helper(TreeNode root, int prefix) {
16+
int val = prefix * 10 + root.val;
17+
if (root.left == null && root.right == null)
18+
return val;
19+
else
20+
return (root.left == null ? 0 : helper(root.left, val)) +
21+
(root.right == null ? 0 : helper(root.right, val));
22+
}
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val) {
4+
* this.val = val;
5+
* this.left = this.right = null;
6+
* }
7+
*/
8+
/**
9+
* @param {TreeNode} root
10+
* @return {number}
11+
*/
12+
var sumNumbers = function(root) {
13+
return root ? helper(root, 0) : 0
14+
};
15+
16+
var helper = function (root, prefix) {
17+
if (!root.left && !root.right)
18+
return prefix * 10 + root.val
19+
else
20+
return (root.left ? helper(root.left, prefix * 10 + root.val) : 0) + (root.right ? helper(root.right, prefix * 10 + root.val) : 0)
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode(object):
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution(object):
9+
def sumNumbers(self, root):
10+
"""
11+
:type root: TreeNode
12+
:rtype: int
13+
"""
14+
return self.helper(root, 0) if root else 0
15+
16+
def helper(self, root, prefix):
17+
if not root.left and not root.right:
18+
return prefix * 10 + root.val
19+
else:
20+
return (self.helper(root.left, prefix * 10 + root.val) if root.left else 0) + (self.helper(root.right, prefix * 10 + root.val) if root.right else 0)

0 commit comments

Comments
 (0)