题目描述
  输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。
思路
  使用递归的思想。步骤如下:
 1. 将左子树构造成双链表,并返回链表头节点。
 2. 定位至左子树双链表最后一个节点。
 3. 如果左子树链表不为空的话,将当前root追加到左子树链表。
 4. 将右子树构造成双链表,并返回链表头节点。
 5. 如果右子树链表不为空的话,将该链表追加到root节点之后。
 6. 根据左子树链表是否为空确定返回的节点。
代码实现
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 
 | 
 
 
 
 
 
 
 
 
 
 
 
 public class BinaryTreeLinkedList {
 public TreeNode Convert(TreeNode root) {
 if (root == null) {
 return null;
 }
 
 if (root.left == null && root.right == null) {
 return root;
 }
 TreeNode left = Convert(root.left);
 TreeNode p = left;
 while (p != null && p.right != null) {
 p = p.right;
 }
 
 if (left != null) {
 p.right = root;
 root.left = p;
 }
 
 TreeNode right = Convert(root.right);
 
 if (right != null) {
 root.right = right;
 right.left = root;
 }
 return left != null ? left : root;
 }
 }
 
 |