​https://leetcode-cn.com/problems/insert-into-a-binary-search-tree/​

701. 二叉搜索树中的插入操作_二叉树

/**
* 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,null,null);
}
if(val<root.val){
root.left = insertIntoBST(root.left,val);
}
else{
root.right=insertIntoBST(root.right,val);
}
return root;
}
}