Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 897957e

Browse files
committed
Add solution 129.
1 parent d613c6f commit 897957e

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* public $val = null;
5+
* public $left = null;
6+
* public $right = null;
7+
* function __construct($value) { $this->val = $value; }
8+
* }
9+
*/
10+
class Solution {
11+
12+
/**
13+
* @param TreeNode $root
14+
* @return Integer
15+
*/
16+
function sumNumbers($root) {
17+
return self::getSum($root,0);
18+
19+
}
20+
21+
function getSum($root, $sum){
22+
23+
if($root == null)
24+
return 0;
25+
26+
$sum = $sum * 10 + $root->val;
27+
28+
if($root->left == null && $root->right == null)
29+
return $sum;
30+
31+
return self::getSum($root->left,$sum) + self::getSum($root->right,$sum);
32+
33+
}
34+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /