1. 程式人生 > >劍指offer- 二叉搜尋樹與雙向連結串列

劍指offer- 二叉搜尋樹與雙向連結串列

題目描述

輸入一棵二叉搜尋樹,將該二叉搜尋樹轉換成一個排序的雙向連結串列。要求不能建立任何新的結點,只能調整樹中結點指標的指向。


解題思路

private TreeNode pre = null;
private TreeNode head = null;

public TreeNode Convert(TreeNode root) {
    if (root == null) return null;
    inOrder(root);
    return head;
}

private void inOrder(TreeNode node) {
    if (node == null
) return; inOrder(node.left); node.left = pre; if (pre != null) pre.right = node; pre = node; if (head == null) head = node; inOrder(node.right); }