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 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.

I came across this interview during one of my interviews. A similar problem is discussed in this post. The problem here is simpler as we don’t need to create circular DLL, but a simple DLL. The idea behind its solution is quite simple and straight.

1. If left subtree exists, process the left subtree
…..1.a) Recursively convert the left subtree to DLL.
…..1.b) Then find inorder predecessor of root in left subtree (inorder predecessor is rightmost node in left subtree).
…..1.c) Make inorder predecessor as previous of root and root as next of inorder predecessor.
2. If right subtree exists, process the right subtree (Below 3 steps are similar to left subtree).
…..2.a) Recursively convert the right subtree to DLL.
…..2.b) Then find inorder successor of root in right subtree (inorder successor is leftmost node in right subtree).
…..2.c) Make inorder successor as next of root and root as previous of inorder successor.
3. Find the leftmost node and return it (the leftmost node is always head of converted DLL).

Below is the source code for above algorithm.

// A C++ program for in-place conversion of Binary Tree to DLL
#include <stdio.h> /* A binary tree node has data, and left and right pointers */
struct node
{
int data;
node* left;
node* right;
}; /* This is the core function to convert Tree to list. This function follows
steps 1 and 2 of the above algorithm */
node* bintree2listUtil(node* root)
{
// Base case
if (root == NULL)
return root; // Convert the left subtree and link to root
if (root->left != NULL)
{
// Convert the left subtree
node* left = bintree2listUtil(root->left); // Find inorder predecessor. After this loop, left
// will point to the inorder predecessor
for (; left->right!=NULL; left=left->right); // Make root as next of the predecessor
left->right = root; // Make predecssor as previous of root
root->left = left;
} // Convert the right subtree and link to root
if (root->right!=NULL)
{
// Convert the right subtree
node* right = bintree2listUtil(root->right); // Find inorder successor. After this loop, right
// will point to the inorder successor
for (; right->left!=NULL; right = right->left); // Make root as previous of successor
right->left = root; // Make successor as next of root
root->right = right;
} return root;
} // The main function that first calls bintree2listUtil(), then follows step 3
// of the above algorithm
node* bintree2list(node *root)
{
// Base case
if (root == NULL)
return root; // Convert to DLL using bintree2listUtil()
root = bintree2listUtil(root); // bintree2listUtil() returns root node of the converted
// DLL. We need pointer to the leftmost node which is
// head of the constructed DLL, so move to the leftmost node
while (root->left != NULL)
root = root->left; return (root);
} /* Helper function that allocates a new node with the
given data and NULL left and right pointers. */
node* newNode(int data)
{
node* new_node = new node;
new_node->data = data;
new_node->left = new_node->right = NULL;
return (new_node);
} /* Function to print nodes in a given doubly linked list */
void printList(node *node)
{
while (node!=NULL)
{
printf("%d ", node->data);
node = node->right;
}
} /* Driver program to test above functions*/
int main()
{
// Let us create the tree shown in above diagram
node *root = newNode();
root->left = newNode();
root->right = newNode();
root->left->left = newNode();
root->left->right = newNode();
root->right->left = newNode(); // Convert to DLL
node *head = bintree2list(root); // Print the converted list
printList(head); return ;
}

[geeksforgeeks] Convert a given Binary Tree to Doubly Linked List的更多相关文章

  1. Convert a given Binary Tree to Doubly Linked List

    The question and solution are from: http://www.geeksforgeeks.org/convert-given-binary-tree-doubly-li ...

  2. Data Structure Binary Tree: Convert a given Binary Tree to Doubly Linked List

    http://www.geeksforgeeks.org/in-place-convert-a-given-binary-tree-to-doubly-linked-list/ #include &l ...

  3. Convert Binary Search Tree to Doubly Linked List

    Convert a binary search tree to doubly linked list with in-order traversal. Example Given a binary s ...

  4. Data Structure Binary Tree: Convert an arbitrary Binary Tree to a tree that holds Children Sum Property

    http://www.geeksforgeeks.org/convert-an-arbitrary-binary-tree-to-a-tree-that-holds-children-sum-prop ...

  5. [LeetCode] 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 ...

  6. LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List

    原题链接在这里:https://leetcode.com/problems/convert-binary-search-tree-to-sorted-doubly-linked-list/ 题目: C ...

  7. 【LeetCode】426. Convert Binary Search Tree to Sorted Doubly Linked List 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...

  8. 426. Convert Binary Search Tree to Sorted Doubly Linked List把bst变成双向链表

    [抄题]: Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right po ...

  9. [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 ...

随机推荐

  1. 线性结构CT 02-线性结构1 一元多项式的乘法与加法运算

    设计函数分别求两个一元多项式的乘积与和. 输入格式: 输入分2行,每行分别先给出多项式非零项的个数,再以指数递降方式输入一个多项式非零项系数和指数(绝对值均为不超过1000的整数).数字间以空格分隔. ...

  2. Python核心编程--学习笔记--8--条件与循环

    本章讲述if.while.for以及与他们搭配的else.elif.break.continue.pass等语句. 1 if语句 语法:三部分——关键字if.条件表达式.代码块.(记住冒号) if c ...

  3. java-servlet-jsp

    =========================servletjavax.servlet    javax.servlet.http    javax.annotation    javax.ser ...

  4. 【原创】StickHeaderListView的简单实现,解决footerView问题

    1.前言: 前几天用了GitHub上se.emilsjolander.stickylistheaders这个组件,然后发现这个组件的listview不能添加footerView,加了footer后,滑 ...

  5. Oracle Database Concepts:介绍模式对象(Introduction to Schema Objects)

    数据库模式(schema)是数据结构的逻辑容器,被称作模式对象(schema objects) 每一个数据库用户拥有一个和用户名相同的模式,例如hr用户拥有hr模式. 在一个产品数据库中,模式的拥有者 ...

  6. 浅谈Objective-C编译器指令

    ------<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS ...

  7. PAT乙级真题1003. 我要通过!(20)(解题)

    “答案正确”是自动判题系统给出的最令人欢喜的回复.本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”. 得到“答案正确”的条件是: 1 ...

  8. 转载: android 学习架构

    http://www.cnblogs.com/forlina/archive/2011/06/29/2093332.html 引言 通过前面两篇: Android 开发之旅:环境搭建及HelloWor ...

  9. 菜鸟学习Spring——60s学会Spring与Hibernate的集成

    一.概述. Spring与Hibernate的集成在企业应用中是很常用的做法通过Spring和Hibernate的结合能提高我们代码的灵活性和开发效率,下面我就一步一步的给大家讲述Spring如何和H ...

  10. 使用checked关键字处理“溢出”错误

    在进行算术运算时,可以使用checked关键字有效处理溢出错误,使用checked关键字可能对程序的性能会有一点点的影响,这是一种以牺牲性能换取健康的做法. private void button1_ ...