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。

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

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

 public class BinaryTree {
Node head, prev = null;
void binaryTree2DoubleLinkedList(Node root) {
if (root == null) return;
binaryTree2DoubleLinkedList(root.left);
if (prev == null) {
head = root;
} else {
root.left = prev;
prev.right = root;
}
prev = root;
binaryTree2DoubleLinkedList(root.right);
} void printList(Node node) {
while (node != null) {
System.out.print(node.data + " ");
node = node.right;
}
}
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
Node root = new Node();
root.left = new Node();
root.right = new Node();
root.left.left = new Node();
root.left.right = new Node();
root.right.left = new Node(); tree.binaryTree2DoubleLinkedList(root);
tree.printList(tree.head); } public Node binaryTreeToDDL(Node root) {
if (root == null) return null; Node leftHead = binaryTreeToDDL(root.left);
Node rightHead = binaryTreeToDDL(root.right); Node newHead = null; if (leftHead == null) {
newHead = root;
} else {
Node leftEnd = leftHead;
while (leftEnd.right != null) {
leftEnd = leftEnd.right;
}
leftEnd.right = root;
root.left = leftEnd;
newHead = leftHead;
} if (rightHead != null) {
rightHead.left = root;
root.right = rightHead;
}
return newHead;
}
} class Node {
int data;
Node left, right; public Node(int data) {
this.data = data;
left = right = null;
}
}

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

  1. [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 Bin ...

  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. DataGridview 绑定泛型List<T>

    .DataGridView数据绑定对比(DataTable与泛型List): 当DataGridView的DataSource是DataTable的时候,DataTable的数据改变时,DataGri ...

  2. JSP-Servlet的工作流程

    Servlet基础 1.Servlet概述 JSP的前身就是Servlet.Servlet就是在服务器端运行的一段小程序.一个Servlet就是一个Java类,并且可以通过“请求-响应”编程模型来访问 ...

  3. Netbeans 设置模板

    /** * @Description * @author ${user} * @date ${date} ${time} * @copyright ${copyright} */ 工具-->模板 ...

  4. default配置

    log_format main '$remote_addr - $remote_user [$time_local] $request ' '"$status" $body_byt ...

  5. MEAN组合框架搭建教程

    1,我们先走在官方github里面下载个包文件: git clone https://github.com/linnovate/mean.git  (是慢了点) 2,我把这个文件解压后文件名叫mean ...

  6. 详细介绍如何在win7下首次实现通过Git bash向Github提交项目

    详细介绍如何在win7下首次实现通过Git bash向Github提交项目 引自:http://jingpin.jikexueyuan.com/article/35944.html 作者: wddoe ...

  7. spring缓存Ehcache(入门2)源码解读

    Ehcache缓存: 解读: Ehcache缓存是在继承spring缓存核心类CacheManager的基础上实现的. 常用类: EhCacheCacheManager:继承自CacheManager ...

  8. Hadoop Fsimage 和 editlog

    在<Hadoop NameNode元数据相关文件目录解析>文章中提到NameNode的$dfs.namenode.name.dir/current/文件夹的几个文件: 1 current/ ...

  9. 利用session防止用户未经登录而直接访问

    在编写项目的时候,突然想如果按常理出牌,不首先进入登录界面而直接访问网页内容,可不可以呢?如此一来便尝试了一下,整的可以直接进入管理员页面,获取完全的管理权限.于是在网上查看了一下解决方案,学习了一下 ...

  10. Unix操作系统中UUCP知识详细讲解

    导读 Unix操作系统有很多值得学习的地方,这里我们主要介绍Unix操作系统中的uucp,大家一起来学习下吧!UUCP系统是一组程序,完成文件传输,执行系统之间的命令,维护系统使用情况的统计,保护安全 ...