-
Notifications
You must be signed in to change notification settings - Fork 57
/
Solution.java
35 lines (32 loc) · 977 Bytes
/
Solution.java
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
package g0701_0800.s0701_insert_into_a_binary_search_tree;
// #Medium #Tree #Binary_Tree #Binary_Search_Tree #Data_Structure_I_Day_13_Tree
// #Udemy_Tree_Stack_Queue #2022_03_23_Time_0_ms_(100.00%)_Space_54.7_MB_(22.73%)
import com_github_leetcode.TreeNode;
/*
* 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;
* }
* }
*/
public class Solution {
public TreeNode insertIntoBST(TreeNode root, int val) {
if (root == null) {
return new TreeNode(val);
}
if (root.val < val) {
root.right = insertIntoBST(root.right, val);
} else {
root.left = insertIntoBST(root.left, val);
}
return root;
}
}