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 ...
随机推荐
- Zabbix 3.0 安装笔记
Zabbix 3.0 只支持CentOS 7.0以上版本,所以先在虚拟机中安装好CentOS 7.0 x64,并设置好IP,允许虚拟机联网. 1.安装MySQL 从最新版本的linux系统开始,默认的 ...
- (转载)ecshop制作成手机网站的方法
ecshop用手机访问的时候,会自动跳转到 /mobile 目录下,ecshop自带的wap模板是用wml制作的,如果按这种情况,又需要制作一套模板,太麻烦,现在都是智能手机时代,wml模板已经不能 ...
- Mysql函数:Last_insert_id()语法讲解
Mysql函数可以实现许多我们需要的功能,下面介绍的Mysql函数Last_insert_id()就是其中之一,希望对您学习Mysql函数能有所帮助. 自动返回最后一个INSERT或 UPDATE 查 ...
- 【9-15】python学习笔记01
使用#开启行注释: 命令行:使用ctrl+d 退出
- mapreduce 本地调试需要注意的问题
1.写好的程序直接在hadoop集群里面执行 2.如果需要在本地调试,需要注释掉mapred-site.xml <configuration> <!-- <property&g ...
- Emacs配置文件
;;tab and space;;when true,emacs use mixture of tab and space to archieve(setq-default indent-tabs-m ...
- Java 中JOptionPane的基本使用方法
JOptionPane 有助于方便地弹出要求用户提供值或向其发出通知的标准对话框.但是有时候看看API也特别烦,因为方法多,参数多,特别难记忆.这里我给出几种常用的方法供大家参考. (1) publi ...
- PHP简单封装MysqlHelper类
MysqlHelper.class.php 1: <?php 2: 3: /** 4: * Mysql数据帮助类 5: */ 6: class MysqlHelper 7: { 8: func ...
- winServer2003除默认端口外的其他端口只能本地访问,关闭防火墙即可
winServer2003除默认端口外的其他端口只能本地访问,关闭防火墙即可
- js改变HTML元素的值
js改变HTML元素的值(常用,备忘) <!DOCTYPE html> <html> <body> <h1>我的第一段 JavaScript</h ...