File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 20
20
* }
21
21
* }
22
22
*/
23
+
24
+ // 迭代法
25
+ class Solution {
26
+
27
+ private $res = [];
28
+ /**
29
+ * @param TreeNode $root
30
+ * @return Integer[]
31
+ */
32
+ function inorderTraversal($root) {
33
+ if (!$root) {
34
+ return [];
35
+ }
36
+
37
+ $res = []; // 存储结果
38
+ $arr = []; // 栈
39
+ $cur = $root;
40
+ while ($cur != null || !empty($arr)) {
41
+ if ($cur != null) { // 将左节点入栈
42
+ array_push($arr, $cur);
43
+ $cur = $cur->left;
44
+ } else { // 出栈,并处理其右节点
45
+ $cur = array_pop($arr);
46
+ $res[] = $cur->val;
47
+ $cur = $cur->right;
48
+ }
49
+ }
50
+ return $res;
51
+ }
52
+ }
53
+
54
+ // 递归法
23
55
class Solution {
24
56
25
57
/**
@@ -39,6 +71,7 @@ class Solution {
39
71
}
40
72
}
41
73
74
+ // 递归法2
42
75
class Solution {
43
76
44
77
private $res = [];
@@ -56,4 +89,5 @@ class Solution {
56
89
return $this->res;
57
90
}
58
91
}
92
+
59
93
```
You can’t perform that action at this time.
0 commit comments