zoukankan      html  css  js  c++  java
  • [LeetCode] 701. Insert into a Binary Search Tree

    Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.

    Note that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return any of them.

    For example, 

    Given the tree:
            4
           / 
          2   7
         / 
        1   3
    And the value to insert: 5
    

    You can return this binary search tree:

             4
           /   
          2     7
         /    /
        1   3 5
    

    This tree is also valid:

             5
           /   
          2     7
         /    
        1   3
             
              4

    Constraints:

    • The number of nodes in the given tree will be between 0 and 10^4.
    • Each node will have a unique integer value from 0 to -10^8, inclusive.
    • -10^8 <= val <= 10^8
    • It's guaranteed that val does not exist in the original BST.

    二叉搜索树中的插入操作。题目即是题意。我这里给出两种做法,迭代和递归。

    迭代

    时间O(h)

    空间O(1)

    Java实现

     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode() {}
     8  *     TreeNode(int val) { this.val = val; }
     9  *     TreeNode(int val, TreeNode left, TreeNode right) {
    10  *         this.val = val;
    11  *         this.left = left;
    12  *         this.right = right;
    13  *     }
    14  * }
    15  */
    16 class Solution {
    17     public TreeNode insertIntoBST(TreeNode root, int val) {
    18         if (root == null) return new TreeNode(val);
    19         TreeNode cur = root;
    20         while (true) {
    21             if (cur.val <= val) {
    22                 if (cur.right != null) {
    23                     cur = cur.right;
    24                 } else {
    25                     cur.right = new TreeNode(val);
    26                     break;
    27                 }
    28             } else {
    29                 if (cur.left != null) {
    30                     cur = cur.left;
    31                 } else {
    32                     cur.left = new TreeNode(val);
    33                     break;
    34                 }
    35             }
    36         }
    37         return root;
    38     }
    39 }

    递归

    时间O(h)

    空间O(n)

    Java实现

     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     int val;
     5  *     TreeNode left;
     6  *     TreeNode right;
     7  *     TreeNode() {}
     8  *     TreeNode(int val) { this.val = val; }
     9  *     TreeNode(int val, TreeNode left, TreeNode right) {
    10  *         this.val = val;
    11  *         this.left = left;
    12  *         this.right = right;
    13  *     }
    14  * }
    15  */
    16 class Solution {
    17     public TreeNode insertIntoBST(TreeNode root, int val) {
    18         if (root == null) {
    19             return new TreeNode(val);
    20         }
    21         if (val > root.val) {
    22             root.right = insertIntoBST(root.right, val);
    23         } else {
    24             root.left = insertIntoBST(root.left, val);
    25         }
    26         return root;
    27     }
    28 }

    LeetCode 题目总结

  • 相关阅读:
    BZOJ 1221 [HNOI2001] 软件开发 费用流_建模
    BZOJ 1180 / 2843 LCT模板题 + 双倍经验
    bzoj 4372: 烁烁的游戏 动态点分治+树链剖分+线段树
    bzoj 3730: 震波 动态点分治+树链剖分+线段树
    luogu P2634 [国家集训队]聪聪可可 点分治
    bzoj 1468: Tree 点分治
    bzoj 1296: [SCOI2009]粉刷匠 动态规划
    bzoj 1293: [SCOI2009]生日礼物 问题转化 + 性质分析 + 滚动数组优化
    BZOJ 1042: [HAOI2008]硬币购物 容斥原理+背包
    matplotlib模块
  • 原文地址:https://www.cnblogs.com/cnoodle/p/13138869.html
Copyright © 2011-2022 走看看