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

  • 相关阅读:
    图像相似度
    二维数组 问题 E: 计算鞍点
    Uva
    Uva
    Uva
    Uva
    Uva
    Uva
    Uva
    【转载】2015 Objective-C 三大新特性 | 干货
  • 原文地址:https://www.cnblogs.com/cnoodle/p/13138869.html
Copyright © 2011-2022 走看看