Skip to content

Commit 5787f2d

Browse files
committed
Add solution 145.
1 parent ab5feea commit 5787f2d

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 postorderTraversal($root) {
17+
$postOrder = [];
18+
$stack = [];
19+
$node = $root;
20+
while (!empty($stack) || $node != null) {
21+
while ($node != null) {
22+
array_push($stack, $node);
23+
array_unshift($postOrder, $node->val);
24+
$node = $node->right;
25+
}
26+
$node = array_pop($stack)->left;
27+
}
28+
return $postOrder;
29+
}
30+
}

0 commit comments

Comments
 (0)