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 ...
随机推荐
- JavaWeb学习总结(一)——JavaWeb开发入门
http://www.cnblogs.com/xdp-gacl/p/3729033.html 只为成功找方法,不为失败找借口! JavaWeb学习总结(一)--JavaWeb开发入门 一.基本概念 1 ...
- GCD 常用代码
GCD 常用代码 体验代码 异步执行任务 - (void)gcdDemo1 { // 1. 全局队列 dispatch_queue_t q = dispatch_get_global_queue(0, ...
- 【采集层】Kafka 与 Flume 如何选择--转自悟性的博文
[采集层]Kafka 与 Flume 如何选择 收藏 悟性 发表于 2年前 阅读 23167 收藏 16 点赞 4 评论 1 摘要: Kafka, Flume 采集层 主要可以使用Flume, Kaf ...
- R获取股票数据
R中好几个Pkg都提供了股票数据的在线下载方法,如果非得在其中找出一个最好的,那么quantmod当之无愧!举一个例子,譬如下载沪市大盘数据,代码可以是: library(quantmod)SSE & ...
- jQuery属性选择器.attr()和.prop()两种方法
在判断表单单选框是否被选中时,通常会想到使用$('#checkbox').attr('checked')来判断,但在一些情况下,你会发现这种方法并不管用,得到的是undefined. 原来jQuery ...
- session 的用法
</head> <body> <?php //session_start();//开启session,必须写在PHP代码最顶端 //HTTP,无状态性 //记录登陆者状态 ...
- 传统BIOS+UEFI 系统引导修复
一. 去网上下载一款pe软件:BIOS+UEFI引导修复工具这个软件支持传统bios和最新的UEFI引导(1)进入PE环境(win也可以,不过引导损坏一般不能进win),打开软件 ...
- H53D旋转-遁地龙卷风
(-1)写在前面 首先图片是我从互联网上下载的,向这位前辈致敬.我用的是chrome49,没有加不同浏览器的前缀,jquery3.0,图片资源放在了我的百度云盘上http://pan.baidu.co ...
- HDU 4923 Room and Moor
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4923 解题报告:给出一个长度为n的只包含0和1的序列,是否存在一个序列Bi满足一下条件: 1. ...
- BZOJ4517——[Sdoi2016]排列计数
求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列恰好有 m 个数是稳定的 满足条件的序列可 ...