231008刷题记录
参考代码代码随想录来刷的
关键词:二叉树、用数组构造二叉树、判断二叉搜索树
1 654.最大二叉树
力扣题目地址
给定一个不重复的整数数组 nums 。 最大二叉树 可以用下面的算法从 nums 递归地构建:
- 创建一个根节点,其值为 nums 中的最大值。
- 递归地在最大值 左边 的 子数组前缀上 构建左子树。
- 递归地在最大值 右边 的 子数组后缀上 构建右子树。
返回 nums 构建的 最大二叉树 。
利用数组构造二叉树。类似
106.从中序与后序遍历序列构造二叉树
思路:
每次操作会从数组中选出一个根节点,数组中根节点左边部分就是左子树,根节点右边部分就是右子树。
class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
return build(nums, 0, nums.length);
}
// 左闭右开
private TreeNode build(int[] nums, int begin, int end) {
if (begin >= end)
return null;
// 找出最大值在数组中的索引
int maxIndex = begin;
int max = nums[begin];
for (int i = begin + 1; i < end; i++) {
if (nums[i] > max) {
max = nums[i];
maxIndex = i;
}
}
TreeNode left = build(nums, begin, maxIndex);
TreeNode right = build(nums, maxIndex + 1, end);
return new TreeNode(max, left, right);
}
}
2 617.合并二叉树
力扣题目链接
给你两棵二叉树: root1 和 root2 。
想象一下,当你将其中一棵覆盖到另一棵之上时,两棵树上的一些节点将会重叠(而另一些不会)。你需要将这两棵树合并成一棵新二叉树。合并的规则是:如果两个节点重叠,那么将这两个节点的值相加作为合并后节点的新值;否则,不为 null 的节点将直接作为新二叉树的节点。
返回合并后的二叉树。
使用迭代法时,不要老想着用两个指针分别指向root1、root2,可以用栈来实现当前访问结点。
迭代法
class Solution {
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
if (root1 == null)
return root2;
if (root2 == null)
return root1;
ArrayDeque<TreeNode> stack = new ArrayDeque<>();
stack.push(root2);
stack.push(root1);
while (!stack.isEmpty()) {
TreeNode node1 = stack.pop();
TreeNode node2 = stack.pop();
node1.val += node2.val;
if (node1.left != null && node2.left != null) {
stack.push(node2.left);
stack.push(node1.left);
} else {
if (node1.left == null)
node1.left = node2.left;
}
if (node1.right != null && node2.right != null) {
stack.push(node2.right);
stack.push(node1.right);
} else {
if (node1.right == null)
node1.right = node2.right;
}
}
return root1;
}
}
递归
class Solution {
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
if (root1 == null)
return root2;
if (root2 == null)
return root1;
root1.val += root2.val;
root1.left = mergeTrees(root1.left, root2.left);
root1.right = mergeTrees(root1.right, root2.right);
return root1;
}
}
3 700.二叉搜索树中的搜索
力扣题目地址
给定二叉搜索树(BST)的根节点 root 和一个整数值 val。
你需要在 BST 中找到节点值等于 val 的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 null 。
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
TreeNode cur = root;
while (cur != null) {
if (cur.val == val)
return cur;
else if (val > cur.val)
cur = cur.right;
else
cur = cur.left;
}
return null;
}
}
4 98.验证二叉搜索树
力扣题目链接
给你一个二叉树的根节点 root ,判断其是否是一个有效的二叉搜索树。
有效 二叉搜索树定义如下:
- 节点的左子树只包含 小于 当前节点的数。
- 节点的右子树只包含 大于 当前节点的数。
- 所有左子树和右子树自身必须也是二叉搜索树。
思路:
二叉搜索树的中序遍历序列是有序的。
迭代法
class Solution {
public boolean isValidBST(TreeNode root) {
ArrayDeque<TreeNode> stack = new ArrayDeque<>();
TreeNode cur = root;
// 为什么要用Long.MIN_VALUE,而不是Integer.MIN_VALUE?
// 因为有一个测试用例,最左下角的那个结点的值就是Integer.MIN_VALUE
// 如果prev的初始值是Integer.MIN_VALUE,那么必定return false。
long prev = Long.MIN_VALUE;
while (cur != null || !stack.isEmpty()) {
if (cur != null) {
stack.push(cur);
cur = cur.left;
continue;
}
cur = stack.pop();
// visit cur
if (cur.val <= prev)
return false;
prev = cur.val;
cur = cur.right;
}
return true;
}
}
递归
class Solution {
private long prev = Long.MIN_VALUE;
public boolean isValidBST(TreeNode root) {
if (root == null)
return true;
if (!isValidBST(root.left))
return false;
if (prev >= root.val)
return false;
else
prev = root.val;
return isValidBST(root.right);
}
}