LC701-中-二叉树搜索树中的插入操作

1 题目

https://leetcode.cn/problems/insert-into-a-binary-search-tree/description/

给定二叉搜索树(BST)的根节点 root 和要插入树中的值 value ,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 输入数据保证:新值和原始二叉搜索树中的任意节点值都不同。

【注意】可能存在多种有效的插入方式,只要树在插入后仍保持为二叉搜索树即可。 你可以返回 任意有效的结果 。

在这里插入图片描述

在这里插入图片描述

2 思路

二叉搜索树的插入方式不是遇到合适的空节点就插入吗?当然也可以把插入的节点当做头节点,然后对树的结构做调整;

但是题目要求任意的一种方案均可,其实找到合适节点插入就行

因此去找满足条件的空节点,然后插入即可

2.1 递归的方式

class Solution {

    public TreeNode insertIntoBST(TreeNode root, int val) {
        if (root == null) {
            // 插入新的值
            return new TreeNode(val);
        }

        // 构建二叉树时,采用返回值的方式
        if (val < root.val) root.left = insertIntoBST(root.left, val);
        if (val > root.val) root.right = insertIntoBST(root.right, val);
        return root;
    }
}

Note:

加入二叉树和构建二叉树类似,函数的返回值是构建好的二叉树。因此一般左右子树都要接收返回的值(递归构建),然后将构建好的二叉树返回。

root = ...
root.left = insertTree(root.left, val);
root.right = insertTree(root.right, val);
return root;

插入二叉搜索树是【二叉树局部遍历】

2.2 迭代法

使用递归法返回构建好的树,可以将新的二叉树的指针连接到树上,就是上次节点接住了递归函数的返回值。

迭代的方式,如果只使用当前节点,当迭代到需要插入的空位时,已经丢失了父节点的值;因此还要引入前向指针,保存父节点。

class Solution {

    public TreeNode insertIntoBST(TreeNode root, int val) {
        // 边界条件
        if (root == null) {
            return new TreeNode(val);
        }

        // 进行迭代
        TreeNode prev = null;
        TreeNode cur = root;
        while (cur != null) {
            prev = cur;  // prev保留
            if (cur.val > val) cur = cur.left;
            else cur = cur.right;
        }

        TreeNode node = new TreeNode(val);
        if (prev.val < val) prev.right = node;
        else prev.left = node;
        // 返回最后构建好的树
        return root;
    }

}

Note:
类似双指针法,cur指针走到条件时,prev指针进行操作,最后操作时判断插入节点的方向
最后返回构建好的树:root

ACM 输入输出

class TreeNode {

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

}

// Solution

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
//        Solution solution = new Solution();
        while (in.hasNext()) {
            String[] strNums = in.nextLine().split(" ");
            int val = Integer.parseInt(in.nextLine());
            List<TreeNode> nodes = new LinkedList<>();

            for (String strNum: strNums) {
                if (!strNum.isEmpty()) {
                    if (strNum.equals("null")) {
                        nodes.add(null);
                    } else {
                        nodes.add(new TreeNode(Integer.parseInt(strNum)));
                    }
                }
            }

            TreeNode root = constructTree(nodes);
            TreeNode r = solution.insertIntoBST(root, val);

            preorderTree(r);

        }

    }

    public static TreeNode constructTree(List<TreeNode> nodes) {
        if (!nodes.isEmpty()) {
            TreeNode node = nodes.remove(0);
            if (node != null) {
                node.left = constructTree(nodes);
                node.right = constructTree(nodes);
            }
            return node;
        }
        return null;
    }

    public static void preorderTree(TreeNode root) {
        if (root != null) {
            System.out.print(root.val + " ");
            preorderTree(root.left);
            preorderTree(root.right);
        }
    }
}

/*
test case:
4 2 1 null null 3 null null 7
5
 */


参考

https://programmercarl.com/0701.二叉搜索树中的插入操作.html

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值