leetcode701. 二叉搜索树中的插入操作

1.题目描述:

给定二叉搜索树(BST)的根节点root和要插入树中的值value,将值插入二叉搜索树。返回插入后二叉搜索树的根节点。输入数据保证,新值和原始二叉搜索树中的任意节点值都不同。注意,可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。

2.改变原二叉搜索树结构的解法:

首先,找到插入节点的父节点,也就是中序遍历最后一个小于插入值insNode的节点pre(前驱节点),插入有两种情况:①pre不存在右子树,则直接将插入节点置于其右节点。②pre存在右子树,pre.val改为插入节点val值,使用递归将原来的pre节点再插入到新二叉树中。代码如下,具体细节啥的直接写在代码注释里了。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    private TreeNode pre = null;
    public TreeNode insertIntoBST(TreeNode root, int val) {
        TreeNode insNode = new TreeNode(val);
        if (root == null) return insNode;
        pre = findPre(root, val);//找到插入val后中序遍历的前驱节点
        if (pre == null) {//pre为null有两种情况,插入节点val值小于所有节点或大于所有节点
            TreeNode temp = root;
            TreeNode temp1 = root;
            while (temp.left != null) temp = temp.left;
            while (temp1.right != null) temp1 = temp1.right;
            if (temp.val > val) temp.left = insNode;
            else temp1.right = insNode;
        } else {
            if (pre.right == null) pre.right = insNode;//pre无右子树,直接加上
            else {
                int temp = pre.val;
                pre.val = val;
                pre = null;//递归前一定要把pre重置!!!
                insertIntoBST(root, temp);//递归插入被替换的pre节点
            }
        }
        return root;
    }

    public TreeNode findPre(TreeNode root, int val) {//找到插入节点的父节点
        if (root == null) return null;
        TreeNode left = findPre(root.left, val);
        if (left != null) return left;
        if (root.val < val) pre = root;
        if (root.val > val) return pre;//不重置这里可能会直接返回上次递归得到的pre
        TreeNode right = findPre(root.right, val);
        if (right != null) return right;
        return null;
    }
}

二刷:找到插入节点的父节点的方法稍微简化一下:

public TreeNode inOrder(TreeNode root, int val) {
        if (root == null) return null;
        TreeNode left = inOrder(root.left, val);
        if (left != null) return left;
        if (root.val >= val) return pre; 
        pre = root;
        return inOrder(root.right, val);
    }

3.不改变原二叉搜索树结构的解法(递归):

不改变原二叉搜索树结构的解法关键在于二叉搜索树的特性,加入的位置可以直接找到,必为null的位置,此时保留了原来的树结构,并且这个位置不一定是叶子节点的位置。

class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        if (root == null) return new TreeNode(val);//若当前节点为空,即val找到了其位置,此时创建节点直接返回    
        if (root.val < val) root.right = insertIntoBST(root.right, val);//递归创建右子树
        //插入的val值大,则去右子树继续插入
        else if (root.val > val) root.left = insertIntoBST(root.left, val);//递归创建左子树
        return root;//每次需返回root节点也就是上级递归的左或右子节点
    }
}

二刷:

class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        if (root == null) return new TreeNode(val);
        if (root.val > val) root.left = insertIntoBST(root.left, val);
        else root.right = insertIntoBST(root.right, val);
        return root;
    }
}

4.不改变原二叉搜索树结构的解法(迭代):

class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        if (root == null) return new TreeNode(val);
        TreeNode temp = root;
        TreeNode pre = null;
        while (temp != null) {
            pre = temp;//用于记录待插入节点的父节点
            if (temp.val > val) temp = temp.left;
            else temp = temp.right;
        }
        if (pre.val > val) pre.left = new TreeNode(val);
        else pre.right = new TreeNode(val);
        return root;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值