Skip to content

Commit ab5feea

Browse files
committed
Add solution 144.
1 parent 2b9febe commit ab5feea

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 preorderTraversal($root) {
17+
$list = [];
18+
$stack = [];
19+
array_push($stack, $root);
20+
while(!empty($stack)){
21+
$node = array_pop($stack);
22+
while($node != null){
23+
array_push($list, $node->val);
24+
array_push($stack, $node->right);
25+
$node = $node->left;
26+
}
27+
}
28+
return $list;
29+
}
30+
}

0 commit comments

Comments
 (0)