forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT26.java
More file actions
31 lines (30 loc) · 1015 Bytes
/
T26.java
File metadata and controls
31 lines (30 loc) · 1015 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
package web; /**
* @program LeetNiu
* @description: 二叉搜索树与双向链表
* @author: mf
* @create: 2020/01/13 13:42
*/
/**
* 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。
*/
public class T26 {
public TreeNode Convert(TreeNode pRootOfTree) {
if (pRootOfTree == null) return null;
if (pRootOfTree.left == null && pRootOfTree.right == null) return pRootOfTree;
TreeNode left = Convert(pRootOfTree.left);
TreeNode p = left;
while (p != null && p.right != null) {
p = p.right;
}
if (left != null) {
p.right = pRootOfTree;
pRootOfTree.left = p;
}
TreeNode right = Convert(pRootOfTree.right);
if (right != null) {
pRootOfTree.right = right;
right.left = pRootOfTree;
}
return left != null ? left : pRootOfTree;
}
}