-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinodeLcci.php
More file actions
54 lines (39 loc) · 947 Bytes
/
binodeLcci.php
File metadata and controls
54 lines (39 loc) · 947 Bytes
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
51
52
53
54
<?php
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 $tail;
/**
* @param TreeNode $root
* @return TreeNode
*/
function convertBiNode($root) {
if($root === null){
return [];
}
$dummyHead = new TreeNode();
$this->tail = $dummyHead;
//题目本质为将二叉搜索树中序展开
$this->inorder($root);
return $dummyHead->right;
}
function inorder($root)
{
if($root === null){
return;
}
$this->inorder($root->left);
$this->tail->right = $root;
$this->tail = $root;
$this->tail->left = null;
$this->inorder($root->right);
}
}