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连起来。

  1. public class BinaryTree {
  2. Node head, prev = null;
  3. void binaryTree2DoubleLinkedList(Node root) {
  4. if (root == null) return;
  5. binaryTree2DoubleLinkedList(root.left);
  6. if (prev == null) {
  7. head = root;
  8. } else {
  9. root.left = prev;
  10. prev.right = root;
  11. }
  12. prev = root;
  13. binaryTree2DoubleLinkedList(root.right);
  14. }
  15.  
  16. void printList(Node node) {
  17. while (node != null) {
  18. System.out.print(node.data + " ");
  19. node = node.right;
  20. }
  21. }
  22. public static void main(String[] args) {
  23. BinaryTree tree = new BinaryTree();
  24. Node root = new Node();
  25. root.left = new Node();
  26. root.right = new Node();
  27. root.left.left = new Node();
  28. root.left.right = new Node();
  29. root.right.left = new Node();
  30.  
  31. tree.binaryTree2DoubleLinkedList(root);
  32. tree.printList(tree.head);
  33.  
  34. }
  35.  
  36. public Node binaryTreeToDDL(Node root) {
  37. if (root == null) return null;
  38.  
  39. Node leftHead = binaryTreeToDDL(root.left);
  40. Node rightHead = binaryTreeToDDL(root.right);
  41.  
  42. Node newHead = null;
  43.  
  44. if (leftHead == null) {
  45. newHead = root;
  46. } else {
  47. Node leftEnd = leftHead;
  48. while (leftEnd.right != null) {
  49. leftEnd = leftEnd.right;
  50. }
  51. leftEnd.right = root;
  52. root.left = leftEnd;
  53. newHead = leftHead;
  54. }
  55.  
  56. if (rightHead != null) {
  57. rightHead.left = root;
  58. root.right = rightHead;
  59. }
  60. return newHead;
  61. }
  62. }
  63.  
  64. class Node {
  65. int data;
  66. Node left, right;
  67.  
  68. public Node(int data) {
  69. this.data = data;
  70. left = right = null;
  71. }
  72. }

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. Node.js Tools 1.2 for Visual Studio 2015 released

    https://blogs.msdn.microsoft.com/visualstudio/2016/07/28/node-js-tools-1-2-visual-studio-2015/ What ...

  2. rpm 命令

    这些事rpm的常用参数!!! 你可以在linux下man 一下rpm就知道了!!! 不过是英文的,不然你可以百度一下rpm就知道了额!!! 下面我帮你贴几个!!!!rpm 常用命令1.安装一个包 # ...

  3. log4j2 使用

    转载自 Blog of 天外的星星: http://www.cnblogs.com/leo-lsw/p/log4j2tutorial.html Log4j 2的好处就不和大家说了,如果你搜了2,说明你 ...

  4. Python之路【第三篇补充】:Python基础(三)

    参考老师:http://www.cnblogs.com/wupeiqi lambda表达式 学习条件运算时,对于简单的 if else 语句,可以使用三元运算来表示,即: # 普通条件语句 if 1 ...

  5. mysql分表的3种方法(转)

    一,先说一下为什么要分表 当一张的数据达到几百万时,你查询一次所花的时间会变多,如果有联合查询的话,我想有可能会死在那儿了.分表的目的就在于此,减小数据库的负担,缩短查询时间. 根据个人经验,mysq ...

  6. JavaScript 五种(非构造方式)继承

    参考链接:http://www.ruanyifeng.com/blog/2010/05/object-oriented_javascript_inheritance_continued.html

  7. C# 表达式树demo

    class Program { static void Main(string[] args) { //创建Expression参数 var left = System.Linq.Expression ...

  8. kafka C客户端librdkafka producer源码分析

    from:http://www.cnblogs.com/xhcqwl/p/3905412.html kafka C客户端librdkafka producer源码分析 简介 kafka网站上提供了C语 ...

  9. 今天讲的是JQ 的动画效果

    老规矩,先贴代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...

  10. CF453C Little Pony and Summer Sun Celebration (DFS)

    http://codeforces.com/contest/456  CF454E Codeforces Round #259 (Div. 1) C Codeforces Round #259 (Di ...