力扣701,二叉搜索树中的插入操作

701. 二叉搜索树中的插入操作 - 力扣(LeetCode)

根据二叉搜素树的特性遍历

复制代码
/**
 * 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) {
        TreeNode t  = new TreeNode(val);
        if(root == null){
            return t;
        }
        if(root.val < val){
            root.right = insertIntoBST(root.right,val);
        }
        if(root.val > val){
          root.left=insertIntoBST(root.left,val);
        }
        return root;
    }
}
相关推荐
卡提西亚2 小时前
leetcode-1438. 绝对差不超过限制的最长连续子数组
算法·leetcode·职场和发展
Java面试题总结2 小时前
LeetCode 93.复原IP地址
算法·leetcode·职场和发展·.net
从零开始的代码生活_2 小时前
C++ 多态详解:虚函数、动态绑定、抽象类与虚表原理
开发语言·c++·后端·学习·算法
泷寂3 小时前
最小生成树 (MST基础)
算法
Daniel_1233 小时前
数组——总结篇
算法
不懒不懒3 小时前
【针对路面识别数据集,结合三轴加速度标准化数据及多路面识别需求,以下是算法选择与处理方案】
算法
Reart4 小时前
Leetcode 121. 买卖股票的最佳时机(717)
后端·算法
会编程的小孩4 小时前
初识数据类型以及变量定义
数据结构·算法
Reart4 小时前
Leetcode 337.打家劫舍3(717)
后端·算法
梅梅绵绵冰4 小时前
数据结构-时间复杂度
数据结构·算法