Day20代码随想录算法训练营二叉树part06|654.最大二叉树、合并二叉树、700.二叉搜索树中的搜索、98.验证二叉搜索树
今日内容
- 654.最大二叉树
- 617.合并二叉树
- 700.二叉搜索树中的搜索
- 98.验证二叉搜索树
654.最大二叉树
前序遍历:中左右
1 /* 2 * @lc app=leetcode.cn id=654 lang=javascript 3 * 4 * [654] 最大二叉树 5 */ 6 7 // @lc code=start 8 /** 9 * Definition for a binary tree node. 10 * function TreeNode(val, left, right) { 11 * this.val = (val===undefined ? 0 : val) 12 * this.left = (left===undefined ? null : left) 13 * this.right = (right===undefined ? null : right) 14 * } 15 */ 16 /** 17 * @param {number[]} nums 18 * @return {TreeNode} 19 */ 20 var constructMaximumBinaryTree = function(nums) { 21 if (nums.length === 1) return new TreeNode(nums[0]); //如果数组长度为一 22 // 中 23 let maxval = 0; 24 let index = 0; 25 for (let i = 0; i < nums.length; i++) { 26 if (nums[i] > maxval) { 27 maxval = nums[i]; 28 index = i; 29 } 30 } 31 32 let Node = new TreeNode(maxval); //中间节点 33 // 左递归 34 if (index > 0) { 35 let newvec1 = nums.slice(0, index); 36 Node.left = constructMaximumBinaryTree(newvec1); 37 } 38 // 右递归 39 if (index < nums.length - 1) { 40 let newvec2 = nums.slice(index + 1); 41 Node.right = constructMaximumBinaryTree(newvec2); 42 } 43 return Node; 44 };
617.合并二叉树 (递归法)
1 /* 2 * @lc app=leetcode.cn id=617 lang=javascript 3 * 4 * [617] 合并二叉树 5 */ 6 7 // @lc code=start 8 /** 9 * Definition for a binary tree node. 10 * function TreeNode(val, left, right) { 11 * this.val = (val===undefined ? 0 : val) 12 * this.left = (left===undefined ? null : left) 13 * this.right = (right===undefined ? null : right) 14 * } 15 */ 16 /** 17 * @param {TreeNode} root1 18 * @param {TreeNode} root2 19 * @return {TreeNode} 20 */ 21 var mergeTrees = function(root1, root2) { 22 // 终止条件 23 if (root1 === null) return root2;// 如果t1为空,合并之后就应该是t2 24 if (root2 === null) return root1;// 如果t2为空,合并之后就应该是t1 25 //修改了root1的数值和结构 26 root1.val += root2.val; 27 root1.left = mergeTrees(root1.left, root2.left); 28 root1.right = mergeTrees(root1.right, root2.right); 29 return root1; 30 };
700.二叉搜索树中的搜索
1 /* 2 * @lc app=leetcode.cn id=700 lang=javascript 3 * 4 * [700] 二叉搜索树中的搜索 5 */ 6 7 // @lc code=start 8 /** 9 * Definition for a binary tree node. 10 * function TreeNode(val, left, right) { 11 * this.val = (val===undefined ? 0 : val) 12 * this.left = (left===undefined ? null : left) 13 * this.right = (right===undefined ? null : right) 14 * } 15 */ 16 /** 17 * @param {TreeNode} root 18 * @param {number} val 19 * @return {TreeNode} 20 */ 21 var searchBST = function(root, val) { 22 if (root === null || root.val === val) return root; 23 let result = null; 24 if (val < root.val) result = searchBST(root.left, val); 25 if (val > root.val) result = searchBST(root.right, val); 26 return result; 27 };
98.验证二叉搜索树
1 /* 2 * @lc app=leetcode.cn id=98 lang=javascript 3 * 4 * [98] 验证二叉搜索树 5 */ 6 7 // @lc code=start 8 /** 9 * Definition for a binary tree node. 10 * function TreeNode(val, left, right) { 11 * this.val = (val===undefined ? 0 : val) 12 * this.left = (left===undefined ? null : left) 13 * this.right = (right===undefined ? null : right) 14 * } 15 */ 16 /** 17 * @param {TreeNode} root 18 * @return {boolean} 19 */ 20 var isValidBST = function(root, min = null, max = null) { 21 if (root === null) { 22 return true; // 空树是有效的二叉搜索树 23 } 24 25 if ((min !== null && root.val <= min) || (max !== null && root.val >= max)) { 26 return false; // 当前节点的值不在允许的范围内 27 } 28 29 // 递归检查左右子树,同时更新上下界 30 return ( 31 isValidBST(root.left, min, root.val) && 32 isValidBST(root.right, root.val, max) 33 ); 34 };
注意:二叉搜索树的性质要求左子树中的所有节点的值都小于根节点的值,右子树中的所有节点的值都大于根节点的值

浙公网安备 33010602011771号