跳转至

701. Insert into a Binary Search Tree

701. Insert into a Binary Search Tree - Medium

二叉搜索树的题目都大同小异,基本上一个模版就能搞定。

Description

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.

题意就是让我们将一个数插入到二叉搜索树中,因为二叉搜索树的特性,这道题就很简单了,直接上模版。一般递归就能解决

Solution

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

    private void insert(TreeNode node, int val) {
        if (node.val < val) {
            if (node.right != null) {
                insert(node.right, val);
            } else {
                node.right = new TreeNode(val);
            }
        } else {
            if (node.left != null) {
                insert(node.left, val);
            } else {
                node.left = new TreeNode(val);
            }
        }
    }
}

评论

回到页面顶部