• [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 题目总结

  • 相关阅读:
    《我是一只IT小小鸟》
    &&、||、?:、,四个运算符的求值顺序
    C Traps and Pitfalls 练习4.2
    “检测到LoaderLock”的解决办法
    VS中代码对齐等快捷键
    贪心 Greedy Algorithms
    这些最基本的程序优化方法你用过吗?
    内存区划分、内存分配、常量存储区、堆、栈、自由存储区、全局区[C++][内存管理][转载]
    [原创]让对话框的控件支持tooltips
    Debug 运行正常但 Release 失败的问题,Debug 和 Release 编译方式的本质区别
  • 原文地址:https://www.cnblogs.com/cnoodle/p/13138869.html
Copyright © 2020-2023  润新知