二分搜索树-二叉搜索树中的插入(领扣)

在这里插入图片描述

/**
 * 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){
            return 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 root;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43283092/article/details/89486761