二叉搜索树与双向链表

题目

https://leetcode-cn.com/problems/er-cha-sou-suo-shu-yu-shuang-xiang-lian-biao-lcof

解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Node head, pre;
public Node treeToDoublyList(Node root) {
if (root == null) return null;
dfs(root);
head.left = pre;
pre.right = head;
return head;
}

void dfs(Node cur) {
if (cur == null) return;
dfs(cur.left);
// 构建链表
if (pre != null) {
pre.right = cur;

} else {
head = cur;
}
cur.left = pre;
pre = cur;
dfs(cur.right);
}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×