-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolutionmian36.java
More file actions
80 lines (58 loc) · 2.55 KB
/
Solutionmian36.java
File metadata and controls
80 lines (58 loc) · 2.55 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package Leetcode;
/**
* @author zbl
* @version 1.0
* @content:输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的循环双向链表。要求不能创建任何新的节点,只能调整树中节点指针的指向。
为了让您更好地理解问题,以下面的二叉搜索树为例:
我们希望将这个二叉搜索树转化为双向循环链表。链表中的每个节点都有一个前驱和后继指针。对于双向循环链表,第一个节点的前驱是最后一个节点,最后一个节点的后继是第一个节点。
下图展示了上面的二叉搜索树转化成的链表。“head” 表示指向链表中有最小元素的节点。
特别地,我们希望可以就地完成转换操作。当转化完成以后,树中节点的左指针需要指向前驱,树中节点的右指针需要指向后继。还需要返回链表中的第一个节点的指针。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/er-cha-sou-suo-shu-yu-shuang-xiang-lian-biao-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
* @date 2020/6/2 21:01
*/
public class Solutionmian36 {
public static class Node {
public int value;
public Node right;
public Node left;
public Node(int value)
{
this.value=value;
}
}
public static Node treeToDoublyList(Node root) {
//递归写法
if(root==null) return null;
Node[] res=process(root);
res[0].left=res[1];
res[1].right=res[0];
return res[0];
}
//返回以root作为根节点形成的双向链表的头结点以及最后一个结点
private static Node[] process(Node root){
Node[] res=new Node[2];
if(root==null) return res;
Node[] left=process(root.left);
Node[] right=process(root.right);
if(left[1]!=null)
left[1].right=root;
root.left=left[1];
root.right=right[0];
if(right[0]!=null)
right[0].left=root;
res[0]=left[0]!=null ? left[0]:root;//当子树返回的结果不为null的时候才能使res[0]=left[0]
res[1]=right[1]!=null ? right[1] : root ;//与上面同理
return res;
}
public static void main(String[] args) {
Node head = new Node(4);
head.left = new Node(2);
head.right = new Node(6);
head.left.left = new Node(1);
head.left.right = new Node(3);
head.right.left = new Node(5);
treeToDoublyList(head);
}
}