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. 事务环境下的CombGuid

    一直使用osharp,osharp3使用的是combguid,代码如下 /// <summary> /// 返回Guid用于数据库操作,特定的时间代码可以提高检索效率 /// </s ...

  2. js实现点击增加文本输入框

    html代码: <ul id="ulid21" > <li id="li11" >问卷选项设置:</li> </ul& ...

  3. Java多线程编程核心技术--Lock的使用(一)

    使用ReentrantLock类 在Java多线程中,可以使用synchronized关键字来实现线程之间的同步互斥,但在JDK1.5中新增加了ReentrantLock类也能达到同样的效果,并且在扩 ...

  4. java中如何将字符串数组转换成字符串(转)

    如果是 “字符串数组” 转 “字符串”,只能通过循环,没有其它方法 String[] str = {"abc", "bcd", "def"} ...

  5. javascript的对象 和 JSON 对象?

    关于js和JSON的 一篇 好文章: http://www.68idc.cn/help/makewebs/javascript/20150704416007.html 讲的很好 很易懂 在将javas ...

  6. vijos1740 聪明的质监员 (二分、区间求和)

    http://www.rqnoj.cn/problem/657 https://www.vijos.org/p/1740 P1740聪明的质检员 请登录后递交 标签:NOIP提高组2011[显示标签] ...

  7. Job中织梦标签的调用

    织梦CMS是一个好东东, 可以让一个网站更好维护和管理, 唯一让我感到忧桑的就是经常在搭后台的时候记不住那些标签,,无奈只能去看手册,有相同的案例直接COPY过来,直接用就OK~~~其实CMS这个东西 ...

  8. 思维导图-javascript(转)

    学习的道路就是要不断的总结归纳,好记性不如烂笔头,so,下面将po出8张javascript相关的思维导图. 思维导图小tips:思维导图又叫心智图,是表达发射性思维的有效的图形思维工具 ,它简单却又 ...

  9. jQuery 2.0发布,不再支持IE6/7/8

    有时发现jQuery库引用的都对,javascript代码写的也没问题,可是jquery就是出现问题,额--我发现换个jquery库就没问题了,长时间不关注jquery的问题而已: 很多人都没有使用最 ...

  10. 繁华模拟赛day8 科技树

    /* 贪心,很明显是越容易升级的越先升级 */ #include<iostream> #include<cstdio> #include<string> #incl ...