654.最大二叉树
- 刷题https://leetcode.cn/problems/maximum-binary-tree/description/
- 文章讲解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/?vd_source=af4853e80f89e28094a5fe1e220d9062
-
题解(递归法):
/*** 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 constructMaximumBinaryTree(int[] nums) {return constructMaximumBinaryTree1(nums, 0, nums.length);}public TreeNode constructMaximumBinaryTree1(int[] nums, int indexBegin, int indexEnd){//递归出口处理//左闭右开结构//若没有元素,直接返回nullif(indexBegin >= indexEnd){return null;}//若只有一个元素,直接将当前元素创建新结点并加入二叉树构建中if(indexEnd - indexBegin == 1){return new TreeNode(nums[indexBegin]);}//中处理//记录最大值的索引int maxIndex = indexBegin;//记录最大值int maxValue = nums[maxIndex];//遍历最大值之后的所有列表元素,更新当前序列中的最大值及最大值索引for(int i = indexBegin + 1; i < indexEnd; i++){if(nums[i] > maxValue){maxValue = nums[i];maxIndex = i;}}//为最大值加入新建的二叉树创建新结点TreeNode root = new TreeNode(maxValue);//以maxIndex为界划分左右子树进行递归(注意利用左闭右开挖掉分界结点)//左处理root.left = constructMaximumBinaryTree1(nums, indexBegin, maxIndex);//右处理root.right = constructMaximumBinaryTree1(nums, maxIndex + 1, indexEnd);//返回根结点即可return root;}
}
617.合并二叉树
- 刷题https://leetcode.cn/problems/merge-two-binary-trees/description/
- 文章讲解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/?vd_source=af4853e80f89e28094a5fe1e220d9062
-
题解(递归,前序遍历):
/*** 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 mergeTrees(TreeNode root1, TreeNode root2) {//当root1为空,返回root2if(root1 == null){return root2;}//同上if(root2 == null){return root1;}TreeNode merged = new TreeNode(root1.val + root2.val);merged.left = mergeTrees(root1.left, root2.left);merged.right = mergeTrees(root1.right, root2.right);return merged;}
}
700.二叉搜索树中的搜索
- 刷题https://leetcode.cn/problems/search-in-a-binary-search-tree/description/
- 文章讲解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/?spm_id_from=333.788&vd_source=af4853e80f89e28094a5fe1e220d9062
-
题解1(递归法):
/*** 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 searchBST(TreeNode root, int val) {if(root == null || root.val == val){return root;}TreeNode result = new TreeNode();//左处理if(val < root.val){result = searchBST(root.left, val);}//右处理if(val > root.val){result = searchBST(root.right, val);}return result;}
}
/*** 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 searchBST(TreeNode root, int val) {while(root != null){if(val < root.val){root = root.left;}else if(val > root.val){root = root.right;}else{return root;}}return null;}
}
98.验证二叉搜索树
- 刷题https://leetcode.cn/problems/validate-binary-search-tree/description/
- 文章讲解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/?spm_id_from=333.788&vd_source=af4853e80f89e28094a5fe1e220d9062
-
题解(递归法):
/*** 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 {//递归TreeNode max;public boolean isValidBST(TreeNode root) {//二叉搜索树也可以为空if(root == null){return true;}//左递归处理boolean left = isValidBST(root.left);//中处理//用max与root进行双指针比较,max初始为null,初始后第一次被赋值root//此后root与max以双指针形式依次向后比较和遍历//正常二叉搜索树每次都会满足root.val > max.val;//若root.val <= max.val,则说明该二叉树已不是二叉搜索树,返回falseif(max != null && root.val <= max.val){return false;}//当符合要求,root.val > max.val时,双指针继续向后移动max = root;//右处理boolean right = isValidBST(root.right);return right && left;}
}