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。
下面这个方法现在想起来好像并不那么复杂了, 值得学习一下。
这种方法使用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的更多相关文章
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
- 【LeetCode】426. Convert Binary Search Tree to Sorted Doubly Linked List 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leetc ...
- 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 ...
- [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 ...
随机推荐
- nginx basic auth 登陆验证模块
#1. 新建一个pw.pl文件专门用来生成密码 #!/usr/bin/perl use strict; my $pw=$ARGV[0]; print crypt($pw,$pw)."\n&q ...
- 未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPackage”包
解决方案: 关掉VS2012... "Microsoft Visual Studio 2012"->"Visual Studio Tools"->& ...
- CentOS安装wordpress权限问题
最近在CentOS6.5上安装wordpress,遇上一个问题,安装好之后外网总是不能再网页进行配置,想了半天应该是源代码文件的权限问题,具体问题与解决如下: 如果你的wordpress安装目录是wo ...
- StringUtils
StringUtils.hasText(字符串) 如果字符串里面的值为null, "", " ",那么返回值为false:否则为true
- C# Redis消息队列例子
class Program { //版本2:使用Redis的客户端管理器(对象池) public static IRedisClientsManager redisClientManager = ne ...
- [C#基础]c#中的BeginInvoke和EndEndInvoke
摘要 异步这东西,真正用起来的时候,发现事情还是挺多的,最近在项目中用到了异步的知识,发现对它还是不了解,处理起来,走了不少弯路.觉得还是补一补还是很有必要的. MSDN原文地址:https://ms ...
- Linux下的微秒级定时器: usleep, nanosleep, select, pselect
Linux下的微秒级定时器: usleep, nanosleep, select, pselect 标签: linuxnulldelaystructdate 2012-02-07 23:29 4979 ...
- vim 配置,我本机的配置[windows]
set nocompatible source $VIMRUNTIME/vimrc_example.vim source $VIMRUNTIME/mswin.vim behave mswin set ...
- Java当中的内存分配以及值传递问题内存解析
首先必须说明作为Java程序员对于内存只要有大致的了解就可以了,如果你对Java当中的某一个知识点在不需要分析内存分配过程的情况下可以掌握,那就大可不必去研究内存.如果你对知识点已经掌握,那么你应该把 ...
- 2015年11月26日 Java基础系列(三)ThreadLocal类初级学习
序,ThreadLocal类是为了解决多线程的安全问题.线程安全的意思也就是说每个线程操作自己的变量,不要对其他线程的值造成影响. 在很多情况下,ThreadLocal比直接使用synchronize ...