剑指offer最优解Java版

剑指offer最优解Java版-二叉搜索树与双向链表

2019-06-26  本文已影响5人  全菜工程师小辉

题目描述

输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。

第一种方法:

解决方法核心是是中序遍历的非递归算法:修改当前遍历节点与前一遍历节点的指针指向。

class TreeNode {
    int      val   = 0;
    TreeNode left  = null;
    TreeNode right = null;
    public TreeNode(int val) {
        this.val = val;
    }
}

public class Solution {
    public TreeNode Convert(TreeNode root) {
        if (root == null)
            return null;
        Stack<TreeNode> stack = new Stack<TreeNode>();
        TreeNode p = root;
        TreeNode pre = null;// 用于保存双链表序列的上一节点
        boolean isFirst = true;
        while (p != null || !stack.isEmpty()) {
            while (p != null) {
                stack.push(p);
                p = p.left;
            }
            p = stack.pop();
            if (isFirst) {
                root = p;// 将双链表中的第一个节点记为root
                pre = root;
                isFirst = false;
            } else {
                pre.right = p;
                p.left = pre;
                pre = p;
            }
            p = p.right;
        }
        return root;
    }
}

复杂度分析:

第二种方法:递归

class TreeNode {
    int      val   = 0;
    TreeNode left  = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;
    }
}

public class Solution {
    public TreeNode Convert(TreeNode root) {
        if (root == null)
            return null;
        if (root.left == null && root.right == null)
            return root;
        // 1.将左子树构造成双链表,并返回链表头节点
        TreeNode left = Convert(root.left);
        TreeNode p = left;
        // 2.定位至左子树双链表最后一个节点
        while (p != null && p.right != null) {
            p = p.right;
        }
        // 3.如果左子树链表不为空的话,将当前root追加到左子树链表
        if (left != null) {
            p.right = root;
            root.left = p;
        }
        // 4.将右子树构造成双链表,并返回链表头节点
        TreeNode right = Convert(root.right);
        // 5.如果右子树链表不为空的话,将该链表追加到root节点之后
        if (right != null) {
            right.left = root;
            root.right = right;
        }
        // 6.根据左子树链表是否为空确定返回的节点。
        return left != null ? left : root;
    }
}

复杂度分析:

第三种方法:递归

思路与第二种方法一致,仅对第2点中的定位作了修改,新增一个全局变量记录左子树的最后一个节点。

class TreeNode {
    int      val   = 0;
    TreeNode left  = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;
    }
}

public class Solution {
   // 记录子树链表的最后一个节点,终结点只可能为只含左子树的非叶节点与叶节点
    protected TreeNode leftLast = null;

    public TreeNode Convert(TreeNode root) {
        if (root == null)
            return null;
        if (root.left == null && root.right == null) {
            leftLast = root;// 最后的一个节点可能为最右侧的叶节点
            return root;
        }
        // 1.将左子树构造成双链表,并返回链表头节点
        TreeNode left = Convert(root.left);
        // 2.如果左子树链表不为空的话,将当前root追加到左子树链表
        if (left != null) {
            leftLast.right = root;
            root.left = leftLast;
        }
        leftLast = root;// 当根节点只含左子树时,则该根节点为最后一个节点
        // 3.将右子树构造成双链表,并返回链表头节点
        TreeNode right = Convert(root.right);
        // 4.如果右子树链表不为空的话,将该链表追加到root节点之后
        if (right != null) {
            right.left = root;
            root.right = right;
        }
        // 5.根据左子树链表是否为空确定返回的节点。
        return left != null ? left : root;
    }
}

复杂度分析:

哎呀,如果我的名片丢了。微信搜索“全菜工程师小辉”,依然可以找到我
上一篇 下一篇

猜你喜欢

热点阅读