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 fa7774e

Browse files
committed
Feat: binary tree
1 parent a504557 commit fa7774e

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
## 题目
2+
3+
## 思路
4+
5+
## 代码
6+
7+
```php
8+
/**
9+
* Definition for a binary tree node.
10+
* class TreeNode {
11+
* public $val = null;
12+
* public $left = null;
13+
* public $right = null;
14+
* function __construct($val = 0, $left = null, $right = null) {
15+
* $this->val = $val;
16+
* $this->left = $left;
17+
* $this->right = $right;
18+
* }
19+
* }
20+
*/
21+
class Solution {
22+
23+
/**
24+
* @param TreeNode $root
25+
* @return Integer[]
26+
*/
27+
function preorderTraversal($root) {
28+
if (!$root) {
29+
return [];
30+
}
31+
32+
$result[] = $root->val;
33+
$left = $this->preorderTraversal($root->left);
34+
$right = $this->preorderTraversal($root->right);
35+
36+
return array_merge($result, $left, $right);
37+
}
38+
}
39+
```
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
## 题目
2+
3+
## 思路
4+
5+
## 代码
6+
7+
```php
8+
/**
9+
* Definition for a binary tree node.
10+
* class TreeNode {
11+
* public $val = null;
12+
* public $left = null;
13+
* public $right = null;
14+
* function __construct($val = 0, $left = null, $right = null) {
15+
* $this->val = $val;
16+
* $this->left = $left;
17+
* $this->right = $right;
18+
* }
19+
* }
20+
*/
21+
class Solution {
22+
23+
/**
24+
* @param TreeNode $root
25+
* @return Integer[]
26+
*/
27+
function postorderTraversal($root) {
28+
if (!$root) {
29+
return [];
30+
}
31+
$left = $this->postorderTraversal($root->left);
32+
$right = $this->postorderTraversal($root->right);
33+
$result[] = $root->val;
34+
35+
return array_merge($left, $right, $result);
36+
}
37+
}
38+
39+
```
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
## 题目
2+
3+
## 思路
4+
5+
## 代码
6+
7+
```php
8+
/**
9+
* Definition for a binary tree node.
10+
* class TreeNode {
11+
* public $val = null;
12+
* public $left = null;
13+
* public $right = null;
14+
* function __construct($val = 0, $left = null, $right = null) {
15+
* $this->val = $val;
16+
* $this->left = $left;
17+
* $this->right = $right;
18+
* }
19+
* }
20+
*/
21+
class Solution {
22+
23+
/**
24+
* @param TreeNode $root
25+
* @return Integer[]
26+
*/
27+
function inorderTraversal($root) {
28+
if (!$root) {
29+
return [];
30+
}
31+
32+
$left = $this->inorderTraversal($root->left);
33+
$result[] = $root->val;
34+
$right = $this->inorderTraversal($root->right);
35+
36+
return array_merge($left, $result, $right);
37+
}
38+
}
39+
```

0 commit comments

Comments
(0)

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