Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 192451d

Browse files
committedDec 5, 2021
剑指 Offer 36. 二叉搜索树与双向链表
1 parent c2972ff commit 192451d

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
 
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.geekidentity.leetcode.offer.backtracking;
2+
3+
/**
4+
* 搜索与回溯算法
5+
* 剑指 Offer 36. 二叉搜索树与双向链表
6+
* https://leetcode-cn.com/problems/er-cha-sou-suo-shu-yu-shuang-xiang-lian-biao-lcof/
7+
*/
8+
public class Offer36TreeToDoublyList {
9+
private Node pre, head;
10+
11+
public Node treeToDoublyList(Node root) {
12+
if (root == null) {
13+
return null;
14+
}
15+
dfs(root);
16+
head.left = pre;
17+
pre.right = head;
18+
return head;
19+
}
20+
21+
private void dfs(Node cur) {
22+
if (cur == null) {
23+
return;
24+
}
25+
dfs(cur.left);
26+
if (head == null) {
27+
head = cur;
28+
} else {
29+
pre.right = cur;
30+
}
31+
cur.left = pre;
32+
pre = cur;
33+
dfs(cur.right);
34+
}
35+
36+
private static class Node {
37+
public int val;
38+
public Node left;
39+
public Node right;
40+
41+
public Node() {}
42+
43+
public Node(int _val) {
44+
val = _val;
45+
}
46+
47+
public Node(int _val,Node _left,Node _right) {
48+
val = _val;
49+
left = _left;
50+
right = _right;
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)
Please sign in to comment.