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);
};