将二叉排序树转换成排序的双向链表

tag: 小米面试题
备注: 小米经典面试题, 多个朋友不同时期,不同研发岗位面试中都碰到过

思路:
(1)二叉序的中序遍历后有序
(2)用一个pre指针辅助

思路二:递归如何做???


  1 package com.zhaochao.tree;
  2 
  3 import java.util.ArrayList;
  4 import java.util.Stack;
  5 
  6 /**
  7  * Created by zhaochao on 17/1/23.
  8  */
  9 public class BST2DLL {
 10 
 11     public ArrayList<TreeNode> bst = new ArrayList<TreeNode>();
 12 
 13 
 14     // 中序遍历
 15     public ArrayList<Integer> inorder(TreeNode root) {
 16         ArrayList<Integer> result = new ArrayList<Integer>();
 17         if(root == null) {
 18             return result;
 19         }
 20 
 21         Stack<TreeNode> stack = new Stack<TreeNode>();
 22 
 23         while(root != null || !stack.isEmpty()) {
 24             while(root != null) {
 25                 stack.push(root);
 26                 root = root.left;
 27             }
 28             TreeNode node = stack.peek();
 29             result.add(node.val);
 30             stack.pop();
 31             root = node.right;
 32         }
 33         return result;
 34     }
 35 
 36 
 37     /*
 38     中序遍历过程中进行指针修改
 39     用一个pre指针指向left孩子
 40     current node 的右孩子为右结点的左左下结点,所以在下一次遍历时修改即可。 pre.next = current node 有点技巧
 41     if(pre != null) { pre.right = node; }
 42     pre = node;
 43     */
 44 
 45     public void bst2DLL(TreeNode root) {
 46 
 47         if(root == null) {
 48             return;
 49         }
 50 
 51         TreeNode pre = null;
 52         Stack<TreeNode> stack = new Stack<TreeNode>();
 53         while(root != null || !stack.isEmpty()) {
 54             while(root != null) {
 55                 stack.push(root);
 56                 root = root.left;
 57             }
 58             TreeNode node = stack.peek();
 59             bst.add(node);
 60             node.left = pre;
 61             if(pre != null) {
 62                 pre.right = node;
 63             }
 64             pre = node;
 65             stack.pop();
 66             root = node.right;
 67         }
 68     }
 69 
 70     public static void main(String[] args) {
 71 
 72         TreeNode root = new TreeNode(0);
 73         TreeNode node1 = new TreeNode(1);
 74         TreeNode node2 = new TreeNode(2);
 75         TreeNode node3 = new TreeNode(3);
 76 
 77         root.left = node1;
 78         root.right = node2;
 79         node2.left = node3;
 80 
 81         BST2DLL test = new BST2DLL();
 82         test.bst2DLL(root);
 83 
 84         for(int i = 0 ; i < test.bst.size(); i++) {
 85             System.out.print(test.bst.get(i).val + " ");
 86             if(test.bst.get(i).left != null) {
 87                 System.out.print("left = "+test.bst.get(i).left.val + " ");
 88             } else {
 89 
 90                 System.out.print("left = null" + " ");
 91             }
 92             if(test.bst.get(i).right != null) {
 93                 System.out.println("right = "+test.bst.get(i).right.val);
 94             } else {
 95                 System.out.println("right = null");
 96             }
 97         }
 98 
 99     }
100 
101 }

 

posted @ 2017-01-24 05:03  notesbuddy  阅读(1221)  评论(0编辑  收藏  举报