File tree Expand file tree Collapse file tree 1 file changed +53
-0
lines changed
src/main/java/com/geekidentity/leetcode/offer/backtracking Expand file tree Collapse file tree 1 file changed +53
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments