Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f40e1be

Browse files
committed
Feat: 94
1 parent d6d6134 commit f40e1be

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

‎problems/94-binary-tree-inorder-traversal.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,38 @@
2020
* }
2121
* }
2222
*/
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+
// 递归法
2355
class Solution {
2456

2557
/**
@@ -39,6 +71,7 @@ class Solution {
3971
}
4072
}
4173

74+
// 递归法2
4275
class Solution {
4376

4477
private $res = [];
@@ -56,4 +89,5 @@ class Solution {
5689
return $this->res;
5790
}
5891
}
92+
5993
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /