426. Convert a given Binary Tree to Doubly Linked List

The question and solution are from: http://www.geeksforgeeks.org/convert-given-binary-tree-doubly-linked-list-set-3/

Given a Binary Tree (BT), convert it to a Doubly Linked List(DLL) In-Place. The left and right pointers in nodes are to be used as previous and next pointers respectively in converted DLL. The order of nodes in DLL must be same as Inorder of the given Binary Tree. The first node of Inorder traversal (left most node in BT) must be head node of the DLL.

TreeToList

 

曾经的我能够想到的方法就是利用LinkedList来保存每个node,然后修改每个node的left和right。

下面这个方法现在想起来好像并不那么复杂了, 值得学习一下。

这种方法使用inorder traversal, 他先一直traverse到最左边,然后set head. 在traverse右边之前,把当前root设置成prev, 这样就可以把prev和后面部分的dll连起来。

 1 public class BinaryTree {
 2     Node head, prev = null;
 3     void binaryTree2DoubleLinkedList(Node root) {
 4         if (root == null) return;
 5         binaryTree2DoubleLinkedList(root.left);
 6         if (prev == null) { 
 7             head = root;
 8         } else {
 9             root.left = prev;
10             prev.right = root;
11         }
12         prev = root;
13         binaryTree2DoubleLinkedList(root.right);
14     }
15 
16     void printList(Node node) {
17         while (node != null) {
18             System.out.print(node.data + " ");
19             node = node.right;
20         }
21     }
22     public static void main(String[] args) {
23         BinaryTree tree = new BinaryTree();
24         Node root = new Node(10);
25         root.left = new Node(12);
26         root.right = new Node(15);
27         root.left.left = new Node(25);
28         root.left.right = new Node(30);
29         root.right.left = new Node(36);
30 
31         tree.binaryTree2DoubleLinkedList(root);
32         tree.printList(tree.head);
33 
34     }
35     
36     public Node binaryTreeToDDL(Node root) {
37         if (root == null) return null;
38         
39         Node leftHead = binaryTreeToDDL(root.left);
40         Node rightHead = binaryTreeToDDL(root.right);
41         
42         Node newHead = null;
43         
44         if (leftHead == null) {
45             newHead = root;
46         } else {
47             Node leftEnd = leftHead;
48             while (leftEnd.right != null) {
49                 leftEnd = leftEnd.right;
50             }
51             leftEnd.right = root;
52             root.left = leftEnd;
53             newHead = leftHead;
54         }
55         
56         if (rightHead != null) {
57             rightHead.left = root;
58             root.right = rightHead;
59         }
60         return newHead;
61     }
62 }
63 
64 class Node {
65     int data;
66     Node left, right;
67 
68     public Node(int data) {
69         this.data = data;
70         left = right = null;
71     }
72 }

 

Leetcode上的确是真正的doubly linked list. 所以实现上稍微有点不一样。

Convert a Binary Search Tree to a sorted Circular Doubly-Linked List in place.

You can think of the left and right pointers as synonymous to the predecessor and successor pointers in a doubly-linked list. For a circular doubly linked list, the predecessor of the first element is the last element, and the successor of the last element is the first element.

We want to do the transformation in place. After the transformation, the left pointer of the tree node should point to its predecessor, and the right pointer should point to its successor. You should return the pointer to the smallest element of the linked list.

 

Example 1:

Input: root = [4,2,5,1,3]

Output: [1,2,3,4,5]

Explanation: The figure below shows the transformed BST. The solid line indicates the successor relationship, while the dashed line means the predecessor 
 1 class Solution {
 2     public Node treeToDoublyList(Node root) {
 3         if (root == null) {
 4             return null;
 5         }
 6         
 7         Node leftHead = treeToDoublyList(root.left);
 8         Node rightHead = treeToDoublyList(root.right);
 9         root.left = root;
10         root.right = root;
11         return connect(connect(leftHead, root), rightHead);
12     }
13     
14     // Used to connect two circular doubly linked lists. n1 is the head of circular DLL as well as n2.
15     private Node connect(Node n1, Node n2) {
16         if (n1 == null) return n2;
17         if (n2 == null) return n1;
18         
19         Node tail1 = n1.left;
20         Node tail2 = n2.left;
21         
22         tail1.right = n2;
23         n2.left = tail1;
24         tail2.right = n1;
25         n1.left = tail2;
26         
27         return n1;
28     }
29 }

 

posted @ 2016-11-11 07:54  北叶青藤  阅读(579)  评论(0编辑  收藏  举报