654.最大二叉树
题目链接:https://leetcode.cn/problems/maximum-binary-tree/
文档讲解: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
思路
本题要先确定最大的值为根节点,然后左边数组是左子树,右边数组是右子树。和前序遍历(中左右)的顺序一样,所以使用前序遍历的递归法来解决。
代码
class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
return construct(nums);
}
public TreeNode construct (int[] nums) {
if (nums.length == 1) return new TreeNode(nums[0]);
// 中
int index = 0, maxValue = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] > maxValue) {
index = i;
maxValue = nums[i];
}
}
TreeNode root = new TreeNode(maxValue);
// 左
if (index > 0) {
int[] leftNums = new int[index];
for (int i = 0; i < index; i++) {
leftNums[i] = nums[i];
}
root.left = construct(leftNums);
}
// 右
if (index < nums.length - 1) {
int[] rightNums = new int[nums.length - index - 1];
for (int i = 0; i < rightNums.length; i++) {
rightNums[i] = nums[i + index + 1];
}
root.right = construct(rightNums);
}
return root;
}
}
617.合并二叉树
题目链接:https://leetcode.cn/problems/merge-two-binary-trees/
文档讲解: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作为最终结果,root2的值就加在root1上。
代码
class Solution {
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
return getMergeTree(root1, root2);
}
public TreeNode getMergeTree(TreeNode root1, TreeNode root2) {
if (root1 == null) return root2;
if (root2 == null) return root1;
root1.val += root2.val;
root1.left = getMergeTree(root1.left, root2.left);
root1.right = getMergeTree(root1.right, root2.right);
return root1;
}
}
700.二叉搜索树中的搜索
题目链接:https://leetcode.cn/problems/search-in-a-binary-search-tree/
文档讲解: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
思路
- 递归法:递归法中不用强调遍历顺序,因为二叉搜索树自带顺序。
- 迭代法:这道题的迭代法非常简单,比较大小就可以。
代码
递归法
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
return getSearchTree(root, val);
}
public TreeNode getSearchTree(TreeNode root, int val) {
if (root == null) return null;
if (root.val == val) return root;
else if (root.val > val) root = getSearchTree(root.left, val);
else root = getSearchTree(root.right, val);
return root;
}
}
迭代法
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
while (root != null) {
if (root.val > val) root = root.left;
else if (root.val < val) root = root.right;
else return root;
}
return null;
}
}
98.验证二叉搜索树
题目链接:https://leetcode.cn/problems/validate-binary-search-tree/
文档讲解: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
思路
- 本题使用中序遍历(左中右),这样得到的值是递增的,通过判断当前节点是否大于之前节点的最大值,就能得到这个搜索二叉树是否合法。
- 可以用双指针法对代码进行优化,一个指针指向前序节点,就不需要定义
long max = Long.MIN_VALUE;
。
代码
中序遍历
class Solution {
long max = Long.MIN_VALUE;
public boolean isValidBST(TreeNode root) {
if (root == null) return true;
boolean left = isValidBST(root.left);
if (root.val > max) max = root.val;
else return false;
boolean right = isValidBST(root.right);
return left && right;
}
}
双指针优化
class Solution {
TreeNode pre;
public boolean isValidBST(TreeNode root) {
if (root == null) return true;
boolean left = isValidBST(root.left);
if (pre != null && pre.val >= root.val) return false;
pre = root;
boolean right = isValidBST(root.right);
return left && right;
}
}