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

654 最大二叉树

题目链接/文章讲解:https://programmercarl.com/0654.%E6%9C%80%E5%A4%A7%E4%BA%8C%E5%8F%89%E6%A0%91.html
视频讲解:https://www.bilibili.com/video/BV1MG411G7ox
在这里插入图片描述

方法一:自己写的,根据昨天的构建二叉树的思路写的

自己也不知道为什么就对了,递归还是迷糊,这题需要注意Math.max(nums)这样写是错的,因为不是每次都是nums整个数组里找最大的,应该写成Math.max(…nums.slice(start, end+1))

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {number[]} nums
 * @return {TreeNode}
 */
var constructMaximumBinaryTree = function(nums) {
    const helper = (start,end) => {
        if(start > end) return null;
        let max = Math.max(...nums.slice(start, end+1));
        let root = new TreeNode(max);
        let mid = nums.indexOf(max);
        // let leftNum = mid - start;
        root.left = helper(start,mid-1);
        root.right = helper(mid+1,end);
        return root

    }
    return helper(0,nums.length-1);


};

617 合并二叉树

题目链接/文章讲解:https://programmercarl.com/0617.%E5%90%88%E5%B9%B6%E4%BA%8C%E5%8F%89%E6%A0%91.html
视频讲解:https://www.bilibili.com/video/BV1m14y1Y7JK
在这里插入图片描述

方法一:不创建新树,直接在root1上修改

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root1
 * @param {TreeNode} root2
 * @return {TreeNode}
 */
var mergeTrees = function(root1, root2) {
    //在root1上直接修改
    if(root1 === null && root2){
        return root2;
    }
    if((root1 && root2 === null) || (root1===null && root2 === null)){
        return root1;
    }
    root1.val += root2.val;
    root1.left = mergeTrees(root1.left,root2.left);
    root1.right = mergeTrees(root1.right,root2.right);
    return root1;

};

方法二:新建一个树

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root1
 * @param {TreeNode} root2
 * @return {TreeNode}
 */
var mergeTrees = function(root1, root2) {
    //新建一个树
    if(root1 === null && root2){
        return root2;
    }
    if((root1 && root2 === null) || (root1===null && root2 === null)){
        return root1;
    }
    const root = new TreeNode(root1.val + root2.val);
    root.left = mergeTrees(root1.left,root2.left);
    root.right = mergeTrees(root1.right,root2.right);
    return root;

};

700二叉搜索树中的搜索

题目链接/文章讲解: https://programmercarl.com/0700.%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%E4%B8%AD%E7%9A%84%E6%90%9C%E7%B4%A2.html
视频讲解:https://www.bilibili.com/video/BV1wG411g7sF
在这里插入图片描述

方法一:自己写的,用BFS

但没有用到二叉搜索树的特性

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} val
 * @return {TreeNode}
 */
var searchBST = function(root, val) {
    // if(root === null) return [];
    let res = [];
    let queue = [root];
    while(queue.length){
        let len = queue.length;
        let subRes = [];
        while(len){
            let cur = queue.shift();
            if(cur.val === val){
                return cur;
            }else{
                subRes.push(cur.val);
                if(cur.left) queue.push(cur.left);
                if(cur.right) queue.push(cur.right);
                len--;
            }
        }
        res.push(subRes);
    }
    return null;
};

方法二:迭代

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} val
 * @return {TreeNode}
 */
var searchBST = function(root, val) {
    while(root !== null){
        if(root.val > val){
            root = root.left;
        }else if(root.val < val){
            root = root.right;
        }else{
            return root;
        }
    }
    return null;
};

方法三:递归

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @param {number} val
 * @return {TreeNode}
 */
var searchBST = function(root, val) {
    //终止条件
    if(root === null || root.val === val){
        return root;
    }
    if(root.val > val){
        return searchBST(root.left,val);
    }
    if(root.val < val){
        return searchBST(root.right,val);
    }
    return null;
    // if (!root || root.val === val) {
    //     return root;
    // }
    // if (root.val > val)
    //     return searchBST(root.left, val);
    // if (root.val < val)
    //     return searchBST(root.right, val);
};

98 验证二叉搜索树

题目链接/文章讲解:https://programmercarl.com/0098.%E9%AA%8C%E8%AF%81%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91.html
视频讲解:https://www.bilibili.com/video/BV18P411n7Q4

方法一:中序遍历二叉搜索树,判断其是否是升序

中序遍历二叉搜索树得到的数组是升序的,因此仅需要检查该数组是不是升序的就可以验证是不是二叉搜索树

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isValidBST = function(root) {
    if(root === null) return true;
    let arr = [];
    var inorder = function(root){
        if(root === null) return null;
        inorder(root.left);
        arr.push(root.val);
        inorder(root.right);
    }
    inorder(root);
    // console.log([...arr]);
    // console.log([...arr.sort((a,b)=>a-b)]);
    // if([...arr] === [...arr.sort((a,b)=>a-b)]){
    //     return true;
    // }else{
    //     return false;
    // }
    for (let i = 1; i < arr.length; i++) {
        if (arr[i] <= arr[i - 1]) {
            return false;
        }
    }
    return true;
};

方法二:递归,定义pre

这个方法有点没懂

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isValidBST = function(root) {
    let pre = null;
    const inorder = (root) => {
        if(root === null) return true;
        let left = inorder(root.left);
        if(pre !== null && pre.val >= root.val){
            return false;
        }
        pre = root;
        let right = inorder(root.right);
        return left && right;
    }
    return inorder(root);
};
  • 16
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值