2019-11-02 12:07:41 +08:00
|
|
|
|
# 36. 二叉搜索树与双向链表
|
|
|
|
|
|
|
|
|
|
[NowCoder](https://www.nowcoder.com/practice/947f6eb80d944a84850b0538bf0ec3a5?tpId=13&tqId=11179&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking&from=cyc_github)
|
|
|
|
|
|
|
|
|
|
## 题目描述
|
|
|
|
|
|
|
|
|
|
输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。
|
|
|
|
|
|
2019-11-02 17:33:10 +08:00
|
|
|
|
<div align="center"> <img src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/05a08f2e-9914-4a77-92ef-aebeaecf4f66.jpg" width="400"/> </div><br>
|
2019-11-02 12:07:41 +08:00
|
|
|
|
|
|
|
|
|
## 解题思路
|
|
|
|
|
|
|
|
|
|
```java
|
|
|
|
|
private TreeNode pre = null;
|
|
|
|
|
private TreeNode head = null;
|
|
|
|
|
|
|
|
|
|
public TreeNode Convert(TreeNode root) {
|
|
|
|
|
inOrder(root);
|
|
|
|
|
return head;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void inOrder(TreeNode node) {
|
|
|
|
|
if (node == null)
|
|
|
|
|
return;
|
|
|
|
|
inOrder(node.left);
|
|
|
|
|
node.left = pre;
|
|
|
|
|
if (pre != null)
|
|
|
|
|
pre.right = node;
|
|
|
|
|
pre = node;
|
|
|
|
|
if (head == null)
|
|
|
|
|
head = node;
|
|
|
|
|
inOrder(node.right);
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-11-02 17:33:10 +08:00
|
|
|
|
<div align="center"><img width="320px" src="https://cs-notes-1256109796.cos.ap-guangzhou.myqcloud.com/githubio/公众号二维码-2.png"></img></div>
|