[LeetCode] 701. Insert into a Binary Search Tree

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

Notice 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.

Example 1:

Input: root = [4,2,7,1,3], val = 5
Output: [4,2,7,1,3,5]
Explanation: Another accepted tree is:

Example 2:

Input: root = [40,20,60,10,30,50,70], val = 25
Output: [40,20,60,10,30,50,70,null,null,25]

Example 3:

Input: root = [4,2,7,1,3,null,null,null,null,null,null], val = 5
Output: [4,2,7,1,3,5]

Constraints:

  • The number of nodes in the tree will be in the range [0, 104].
  • -108 <= Node.val <= 108
  • All the values Node.val are unique.
  • -108 <= val <= 108
  • It's guaranteed that val does not exist in the original BST.

二叉搜索树中的插入操作。

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

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

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/insert-into-a-binary-search-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

我这里给出两种做法,迭代和递归。

迭代

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

posted @ 2020-06-16 02:38  CNoodle  阅读(181)  评论(0编辑  收藏  举报