Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions problems/二叉树的递归遍历.md
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,62 @@ public void Traversal(TreeNode cur, IList<int> res)
}
```

### PHP
```php
// 144.前序遍历
function preorderTraversal($root) {
$output = [];
$this->traversal($root, $output);
return $output;
}

function traversal($root, array &$output) {
if ($root->val === null) {
return;
}

$output[] = $root->val;
$this->traversal($root->left, $output);
$this->traversal($root->right, $output);
}
```
```php
// 94.中序遍历
function inorderTraversal($root) {
$output = [];
$this->traversal($root, $output);
return $output;
}

function traversal($root, array &$output) {
if ($root->val === null) {
return;
}

$this->traversal($root->left, $output);
$output[] = $root->val;
$this->traversal($root->right, $output);
}
```
```php
// 145.后序遍历
function postorderTraversal($root) {
$output = [];
$this->traversal($root, $output);
return $output;
}

function traversal($root, array &$output) {
if ($root->val === null) {
return;
}

$this->traversal($root->left, $output);
$this->traversal($root->right, $output);
$output[] = $root->val;
}
```


<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
Expand Down