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

题目链接

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

题目描述

给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树。 返回插入后二叉搜索树的根节点。 保证原始二叉搜索树中不存在新值。

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

例如,

给定二叉搜索树:

        4
       /       2   7
     /     1   3

和 插入的值: 5

你可以返回这个二叉搜索树:

         4
       /         2     7
     / \   /
    1   3 5

或者这个树也是有效的:

         5
       /         2     7
     / \
    1   3
                   4

题解

二叉搜索树的插入,依次遍历二叉树,找到插入节点的位置,插入即可。

代码


/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        if (root == null) { return new TreeNode(val); }
        TreeNode cur = root;
        TreeNode pre = null;
        while (cur != null) {
            pre = cur;
            if (cur.val > val) {
                cur = cur.left;
                if (cur == null) {
                    pre.left = new TreeNode(val);
                }
            } else {
                cur = cur.right;
                if (cur == null) {
                    pre.right = new TreeNode(val);
                }
            }
        }
        return root;
    }
}

原文地址:https://www.cnblogs.com/xiagnming/p/9706692.html

时间: 2024-08-28 06:53:56

Leetcode 701. 二叉搜索树中的插入操作的相关文章

leetcode 230二叉搜索树中第k小的元素

通过stack进行中序遍历迭代,timeO(k),spaceO(1) /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ /** BST中序遍历是从小到大排列的因此对其进行中序遍历然后取第k个元素: **/

[LeetCode] 285. Inorder Successor in BST 二叉搜索树中的中序后继节点

Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Note: If the given node has no in-order successor in the tree, return null. 给一个二叉搜索树和它的一个节点,找出它的中序后继节点,如果没有返回null. 解法1: 用中序遍历二叉搜索树,当找到root.val = p.val的时

leetcode 二叉搜索树中第K小的元素 python

二叉搜索树中第K小的元素 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. 示例 1: 输入: root = [3,1,4,null,2], k = 1 3 / 1 4   2 输出: 1 示例 2: 输入: root = [5,3,6,2,4,null,null,1], k = 3 5 / 3 6 / 2 4 / 1 输出: 3 进阶:如果二叉搜索树经常被修改(插入/删除操作)并且

二叉搜索树中的常用方法

1 package Tree; 2 3 import org.junit.Test; 4 5 class TreeNode { 6 7 int val = 0; 8 TreeNode left = null; 9 TreeNode right = null; 10 11 public TreeNode(int val) { 12 this.val = val; 13 14 } 15 16 } 17 18 public class BinarySearchTree { 19 20 /** 21 *

算法dfs——二叉搜索树中最接近的值 II

901. 二叉搜索树中最接近的值 II 中文 English 给定一棵非空二叉搜索树以及一个target值,找到 BST 中最接近给定值的 k 个数. 样例 样例 1: 输入: {1} 0.000000 1 输出: [1] 解释: 二叉树 {1},表示如下的树结构: 1 样例 2: 输入: {3,1,4,#,2} 0.275000 2 输出: [1,2] 解释: 二叉树 {3,1,4,#,2},表示如下的树结构: 3 / 1 4 2 挑战 假设是一棵平衡二叉搜索树,你可以用时间复杂度低于O(n)

[Swift]LeetCode450. 删除二叉搜索树中的节点 | Delete Node in a BST

Given a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST. Basically, the deletion can be divided into two stages: Search for a node to remove. If the n

230. 二叉搜索树中第K小的元素

230. 二叉搜索树中第K小的元素 题意 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. 解题思路 中序遍历,利用Python3中提供的生成器方法: 中序遍历,判断存储结点值的数组是否到到k,则表明访问的一个结点就是第k个最小的元素: 先获取跟结点处于的位置(第几个最小的元素),如果它比k小,则从右子结点中找,如果它比k大,则从左子节点中找: 实现 class Solution:    

230 Kth Smallest Element in a BST 二叉搜索树中第K小的元素

给定一个二叉搜索树,编写一个函数kthSmallest来查找其中第k个最小的元素. 注意:你可以假设k总是有效的,1≤ k ≤二叉搜索树元素个数. 进阶:如果经常修改二叉搜索树(插入/删除操作)并且你需要频繁地找到第k小值呢? 你将如何优化kthSmallest函数? 详见:https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/ 方法一:递归实现 /** * Definition for a binary

二叉搜索树中第K小的元素

给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. 示例 1: 输入: root = [3,1,4,null,2], k = 1 3 / 1 4   2 输出: 1 示例 2: 输入: root = [5,3,6,2,4,null,null,1], k = 3 5 / 3 6 / 2 4 / 1 输出: 3 一个中序遍历的搜索,递归或者栈. /** * Definition for a