diff --git a/solution/0701.Insert into a Binary Search Tree/Solution.java b/solution/0701.Insert into a Binary Search Tree/Solution.java new file mode 100644 index 0000000000000..618569c43cd7a --- /dev/null +++ b/solution/0701.Insert into a Binary Search Tree/Solution.java @@ -0,0 +1,27 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +class Solution { + public TreeNode insertIntoBST(TreeNode root, int val) { + + if(root == null){ + root = new TreeNode(val); + } + + if(val < root.val){ + root.left = insertIntoBST(root.left, val); + } + else if(val > root.val){ + root.right = insertIntoBST(root.right, val); + } + + // return the unchanged pointer + return root; + } +} diff --git a/solution/README.md b/solution/README.md index f2f07a877d2a2..16b79db5d4434 100644 --- a/solution/README.md +++ b/solution/README.md @@ -952,6 +952,7 @@ ├── 0701.Insert into a Binary Search Tree │   ├── README.md │   └── Solution.py +│   └── Solution.java ├── 0703.Kth Largest Element in a Stream │   ├── README.md │   └── Solution.java