/
94. 二叉树的中序遍历.php
50 lines (45 loc) · 1.15 KB
/
94. 二叉树的中序遍历.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
/**
* Created by PhpStorm
* User: jtahstu
* Time: 2022/6/17 10:28
* Desc: 94. 二叉树的中序遍历
* https://leetcode.cn/problems/binary-tree-inorder-traversal/
* 给定一个二叉树的根节点 root ,返回 它的 中序 遍历 。
*/
/**
* Definition for a binary tree node.
* class TreeNode {
* public $val = null;
* public $left = null;
* public $right = null;
* function __construct($val = 0, $left = null, $right = null) {
* $this->val = $val;
* $this->left = $left;
* $this->right = $right;
* }
* }
*/
class Solution {
public $items = [];
/**
* @param TreeNode $root
* @return Integer[]
*/
function inorderTraversal($root) {
$this->dfs($root);
return $this->items;
}
function dfs($root)
{
if ($root == null) return;
$this->dfs($root->left);
$this->items[] = $root->val;
$this->dfs($root->right);
}
}
/**
* 执行用时:8 ms, 在所有 PHP 提交中击败了61.05%的用户
* 内存消耗:18.6 MB, 在所有 PHP 提交中击败了62.21%的用户
* 通过测试用例:70 / 70
*/