We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2b9febe commit ab5feeaCopy full SHA for ab5feea
src/Algorithms/0144.binary-tree-preorder-traversal/binary-tree-preorder-traversal.php
@@ -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