代码随想录算法训练营第十七天| 654.最大二叉树、 617.合并二叉树 、 700.二叉搜索树中的搜索 、 98.验证二叉搜索树

51 阅读2分钟

654.最大二叉树

相关链接:题目链接文章讲解 视频讲解

解题思路

递归结束条件:如果数组是空的,则结束递归,返回null

代码实现

var constructMaximumBinaryTree = function(nums) {
    if(nums.length===1){
       var node = new TreeNode(nums[0]) ;
        return node;
    }
    var maxV = function(nums){
        let res = nums[0]
        for(let i = 1; i< nums.length; i++){
            res = nums[i]>res? nums[i]:res;
        }
        return res;
    }
    var getIndex= function(nums,max){
        var idx =  nums.indexOf(max);
        return idx;
    }
    var dfs = function (nums){
        if (nums.length === 0) {
            return null;
        }
        var max = maxV(nums);
        var idx = getIndex(nums,max);
        var node = new TreeNode(max);
        node.left = dfs(nums.slice(0,idx));
        node.right = dfs(nums.slice(idx + 1));
        return node;
    }
    var node = dfs(nums);
    return node;
};

617.合并二叉树

相关链接:题目链接文章讲解 视频讲解

解题思路

代码实现

var mergeTrees = function(root1, root2) {
    var dfs = function(tree, tree2){
        if(!tree && !tree2){
            return null;
        }
        if(tree || tree2){
            var nums=0;
            if(tree?.val){
                nums +=tree.val;
            }
            if(tree2?.val){
                nums +=tree2.val;
            }
            var node = new TreeNode(nums);
            node.left = dfs(tree && tree.left,tree2 && tree2.left);
            node.right = dfs(tree && tree.right,tree2 && tree2.right);
            return node;
        }
    }
    var node = dfs(root1,root2);
    return node;
};

700.二叉搜索树中的搜索

相关链接:题目链接文章讲解 视频讲解

解题思路

*二叉*查找树(Binary Search Tree),(又:*二叉搜索树*,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值

代码实现

var searchBST = function(root, val) {
    var dfs = function(root){
        if(!root){
            return null;
        }
        if(root.val == val){
            return root;
        }
        if(root.val > val){
            if(root.left) {
                return dfs(root.left)
            }else{
                return null;
            }
        }
        if(root.val < val){
            if(root.right){return dfs(root.right)}else{
               return null;
            }
        }
        
    }
    var res = dfs(root);
    return res;
};

98.验证二叉搜索树

相关链接:题目链接文章讲解 视频讲解

解题思路

*二叉*查找树(Binary Search Tree),(又:*二叉搜索树*,二叉排序树)它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值

代码实现

var isValidBST = function(root) {
    // Helper function to validate BST
    var isBST = function(node, min, max) {
        if (!node) {
            return true; // An empty tree is a valid BST
        }
        
        // Check if the current node's value is within the valid range
        if ((min !== null && node.val <= min) || (max !== null && node.val >= max)) {
            return false;
        }
        
        // Recursively check the left and right subtrees
        // node:6  min:5,max:null
        // node:3 min:5,max:6
        return isBST(node.left, min, node.val) && isBST(node.right, node.val, max);
    };
    
    // Start with the root and no constraints on min and max
    return isBST(root, null, null);
};