---
# System prepended metadata

title: 0701. Insert into a Binary Search Tree
tags: [Leetcode, DFS, Binary Search Tree, Medium]

---

# 0701. Insert into a Binary Search Tree
###### tags: `Leetcode` `Medium` `DFS` `Binary Search Tree`
## 思路 $O(N)$ $O(N)$
## Code
```java=
class Solution {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        if(root==null){
            root=new TreeNode(val);
        }
        else if(root.val>val){
            root.left = insertIntoBST(root.left, val);
        }
        else{
            root.right = insertIntoBST(root.right,val);
        }
        return root;
    }
}
```