如果
value<当前跟节点则插入到左子树中 root.left = insert(root.left,value)
;反之则可插入到右子树中,若相等则丢弃
package FTree;
/**
* @Author Zhou jian
* @Date 2020 ${month} 2020/4/15 0015 11:31
*/
public class Problem701 {
public TreeNode insertIntoBST(TreeNode root, int val) {
if(root==null) return new TreeNode(val);
insert(root,val);
return root;
}
public TreeNode insert(TreeNode root,int val){
//当前节点为空
if(root==null){
return new TreeNode(val);
}
//插入到左子树
if(root.val>val) root.left = insert(root.left,val);
//插入到右子树
else if(root.val<val) root.right = insert(root.right,val);
//相等则返回根节点
return root;
}
}