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.

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

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

 1 public class BinaryTree
 2 {
 3     Node root, head;
 4
 5     // Initialize previously visited node as NULL. This is
 6     // static so that the same value is accessible in all recursive
 7     // calls
 8     Node prev = null;
 9
10     // A simple recursive function to convert a given Binary tree
11     // to Doubly Linked List
12     // root --> Root of Binary Tree
13     void BinaryTree2DoubleLinkedList(Node root) {
14         // Base case
15         if (root == null) return;
16
17         // Recursively convert left subtree
18         BinaryTree2DoubleLinkedList(root.left);
19
20         // Now convert this node
21         if (prev == null){
22             head = root;
23         } else {
24             root.left = prev;
25             prev.right = root;
26         }
27         prev = root;
28
29         // Finally convert right subtree
30         BinaryTree2DoubleLinkedList(root.right);
31     }
32
33     void printList(Node node)
34     {
35         while (node != null)
36         {
37             System.out.print(node.data + " ");
38             node = node.right;
39         }
40     }
41
42     // Driver program to test above functions
43     public static void main(String[] args)
44     {
45         // Let us create the tree as shown in above diagram
46         BinaryTree tree = new BinaryTree();
47         tree.root = new Node(10);
48         tree.root.left = new Node(12);
49         tree.root.right = new Node(15);
50         tree.root.left.left = new Node(25);
51         tree.root.left.right = new Node(30);
52         tree.root.right.left = new Node(36);
53
54         // convert to DLL
55         tree.BinaryTree2DoubleLinkedList(tree.root);
56
57         // Print the converted List
58         tree.printList(tree.head);
59
60     }
61 }
62
63
64 class Node
65 {
66     int data;
67     Node left, right;
68
69     public Node(int data)
70     {
71         this.data = data;
72         left = right = null;
73     }
74 }
时间: 2024-10-16 06:30:08

Convert a given Binary Tree to Doubly Linked List的相关文章

[geeksforgeeks] Convert a given Binary Tree to Doubly Linked List

http://www.geeksforgeeks.org/in-place-convert-a-given-binary-tree-to-doubly-linked-list/ Given a Binary Tree (Bt), convert it to a Doubly Linked List(DLL). The left and right pointers in nodes are to be used as previous and next pointers respectively

Convert a given binary tree to double linked list

public TreeNode binaryTree2List(TreeNode node) { if (node == null) return node; // Convert to DLL using bintree2listUtil() node = bintree2listUtil(node); while (node.left!=null) { node=node.left; } return node; } TreeNode bintree2listUtil(TreeNode no

Convert Binary Search Tree to Doubly Linked List

Convert a binary search tree to doubly linked list with in-order traversal. Have you met this question in a real interview? Yes Example Given a binary search tree: 4 / 2 5 / 1 3 return 1<->2<->3<->4<->5 Runtime: 15ms 1 /** 2 * Defi

刷题感悟 - Convert Binary Search Tree to Doubly Linked List

将二叉查找树转化成双向链表 题目思路其实不难 ,中序遍历,然后再依次的将数据放入链表中即可 重点:新加元素前后链的配置 有个有意思的地方在于result没有赋初值 导致写代码时需要先初始化 然后删除之 . /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val =

426.&#160;Convert Binary Search Tree to Sorted Doubly Linked List

426. Convert Binary Search Tree to Sorted Doubly Linked List https://www.youtube.com/watch?v=FsxTX7-yhOw&t=1210s https://docs.google.com/document/d/1IIn5rXrUumqpxRrMKo76FbBx1ibTBDGso5rfENmkabw/edit class Solution { public Node treeToDoublyList(Node r

[leetcode]426. Convert Binary Search Tree to Sorted Doubly Linked List二叉搜索树转有序双向链表

Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers as synonymous to the previous and next pointers in a doubly-linked list. Let's take the following BST as an example, it may help you understand the p

426. Convert Binary Search Tree to Sorted Doubly Linked List - Medium

Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers as synonymous to the previous and next pointers in a doubly-linked list. Let's take the following BST as an example, it may help you understand the p

[GeeksForGeeks] Extract Leaves of a Binary Tree in a Doubly Linked List

Given a Binary Tree, extract all leaves of it in a Doubly Linked List (DLL). Note that the DLL need to be created in-place. Assume that the node structure of DLL and Binary Tree is same, only the meaning of left and right pointers are different. In D

Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 class Solution { public: TreeNode *node = NULL; void flatten(TreeNode *root) { if(root == NULL) return; i