-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path1022-sum-of-root-to-leaf-binary-numbers.php
155 lines (139 loc) · 4.03 KB
/
1022-sum-of-root-to-leaf-binary-numbers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
// 1022. Sum of Root To Leaf Binary Numbers
// https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 8 ms, faster than 100.00% of PHP online submissions
// for Sum of Root To Leaf Binary Numbers.
// Memory Usage: 16.6 MB, less than 100.00% of PHP online submissions\
// for Sum of Root To Leaf Binary Numbers.
/**
* Definition for a binary tree node.
* class TreeNode {
* public $val = null;
* public $left = null;
* public $right = null;
* function __construct($value) { $this->val = $value; }
* }
*/
// class Solution
// {
// /**
// * @param TreeNode $root
// * @return Integer
// */
// function sumRootToLeaf($root)
// {
// $stack = [[$root, '']];
// $binVals = [];
// while (0 < count($stack)) {
// list($leaf, $acc) = array_pop($stack);
// extract((array)$leaf);
// $acc .= $val;
// if ($left) array_push($stack, [$left, $acc]);
// if ($right) array_push($stack, [$right, $acc]);
// if (!$left && !$right) array_push($binVals, $acc);
// }
// return array_sum(array_map(function ($bin) {
// return bindec($bin);
// }, $binVals));
// }
// }
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Runtime: 12 ms, faster than 50.00% of PHP online submissions
// for Sum of Root To Leaf Binary Numbers.
// Memory Usage: 15.3 MB, less than 100.00% of PHP online submissions
// for Sum of Root To Leaf Binary Numbers.
/**
* Definition for a binary tree node.
* class TreeNode {
* public $val = null;
* public $left = null;
* public $right = null;
* function __construct($value) { $this->val = $value; }
* }
*/
class Solution
{
/**
* @param TreeNode $root
* @return Integer
*/
function sumRootToLeaf($root)
{
$answer = 0;
$stack = [[$root, '']];
while (0 < count($stack)) {
list($leaf, $acc) = array_pop($stack);
extract((array)$leaf);
$acc .= $val;
if ($left) array_push($stack, [$left, $acc]);
if ($right) array_push($stack, [$right, $acc]);
if (!$left && !$right) $answer += bindec($acc);
}
return $answer;
}
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
class TreeNode
{
public $val = null;
public $left = null;
public $right = null;
function __construct($value)
{
$this->val = $value;
}
}
function deserializeLevelOrderArrayAsBinaryTree($a)
{
$nodes = array_map(function ($v) {
return new TreeNode($v);
}, $a);
for ($i = 0; $i < count($nodes); $i++) {
if (!isset($nodes[$i])) continue;
$childIndex = ($i + 1) * 2;
$nodes[$i]->left = $nodes[$childIndex - 1] ?? null;
$nodes[$i]->right = $nodes[$childIndex] ?? null;
}
return $nodes[0];
};
$tests = [
[
// 1
// / \
// 0 1
// / \ / \
// 0 1 0 1
'input' => [1, 0, 1, 0, 1, 0, 1],
'expected' => 22,
// Explanation:
// (100) + (101) + (110) + (111)
// 4 + 5 + 6 + 7 = 22
],
[
// 1
// / \
// 0 1
// / \ / \
// 0 1 0 1
// /
// 1
'input' => [1, 0, 1, 0, 1, 0, 1, 1],
'expected' => 27,
// Explanation:
// (1001) + (101) + (110) + (111)
// 9 + 5 + 6 + 7 = 27
],
];
$s = new Solution();
foreach ($tests as $test) {
extract($test);
$output = $s->sumRootToLeaf(deserializeLevelOrderArrayAsBinaryTree($input));
if ($expected === $output) {
echo '✅ ', implode(', ', $input), '<br>';
} else {
echo '🔴 ', implode(', ', $input), '<br>';
echo "<i>Expected <b>{$expected}</b>, but got <b>{$output}</b></i>";
}
echo '<hr>';
}