题目列表
解题过程
1、654.最大二叉树
给定一个不重复的整数数组 nums
。 最大二叉树 可以用下面的算法从 nums
递归地构建:
- 创建一个根节点,其值为
nums
中的最大值。 - 递归地在最大值 左边 的 子数组前缀上 构建左子树。
- 递归地在最大值 右边 的 子数组后缀上 构建右子树。
返回 nums
构建的 最大二叉树 。
思路: 构造树一般采用的是前序遍历,因为先构造中间节点,然后递归构造左子树和右子树。
- 递归时,先要找到数组中最大的值和对应的下标,最大的值构造根节点,下标用来下一步分割数组;
- 最大值所在的下标左区间用来构造左子树;
- 最大值所在的下标右区间用来构造右子树。
class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
return constructBinaryTree(nums, 0, nums.length);
}
public TreeNode constructBinaryTree(int[] nums, int leftIndex, int rightIndex) {
if (rightIndex - leftIndex < 1) { //没有元素
return null;
}
if (rightIndex - leftIndex == 1) { //有且仅有一个元素
return new TreeNode(nums[leftIndex]);
}
int maxIndex = leftIndex; //初始化最大元素位置
int maxVal = nums[maxIndex]; //最大元素值
for (int i = leftIndex + 1; i < rightIndex; i++) {
if (maxVal < nums[i]) {
maxIndex = i;
maxVal = nums[i];
}
}
TreeNode root = new TreeNode(maxVal);
root.left = constructBinaryTree(nums, leftIndex, maxIndex);
root.right = constructBinaryTree(nums, maxIndex + 1, rightIndex);
return root;
}
}
2、617.合并二叉树
给你两棵二叉树: root1
和 root2
。
想象一下,当你将其中一棵覆盖到另一棵之上时,两棵树上的一些节点将会重叠(而另一些不会)。你需要将这两棵树合并成一棵新二叉树。合并的规则是:如果两个节点重叠,那么将这两个节点的值相加作为合并后节点的新值;否则,不为 null 的节点将直接作为新二叉树的节点。
返回合并后的二叉树。
注意: 合并过程必须从两个树的根节点开始。
思路: 这里使用前中后序遍历都可以,不过前序遍历是最好理解的。
递归法
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;
}
}
使用栈迭代
class Solution {
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
if (root1 == null) {
return root2;
}
if (root2 == null) {
return root1;
}
Stack<TreeNode> stack = new Stack<>(); //先进后出
stack.push(root2);
stack.push(root1);
while (!stack.isEmpty()) {
TreeNode node1 = stack.pop();
TreeNode node2 = stack.pop();
node1.val += node2.val;
if (node2.right != null && node1.right != null) {
stack.push(node2.right);
stack.push(node1.right);
} else if (node1.right == null){
node1.right = node2.right;
}
if (node2.left != null && node1.left != null) {
stack.push(node2.left);
stack.push(node1.left);
} else if (node1.left == null){
node1.left = node2.left;
}
}
return root1;
}
}
使用队列迭代
class Solution {
// 使用队列迭代
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
if (root1 == null) return root2;
if (root2 ==null) return root1;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root1);
queue.offer(root2);
while (!queue.isEmpty()) {
TreeNode node1 = queue.poll();
TreeNode node2 = queue.poll();
// 此时两个节点一定不为空,val相加
node1.val = node1.val + node2.val;
// 如果两棵树左节点都不为空,加入队列
if (node1.left != null && node2.left != null) {
queue.offer(node1.left);
queue.offer(node2.left);
}
// 如果两棵树右节点都不为空,加入队列
if (node1.right != null && node2.right != null) {
queue.offer(node1.right);
queue.offer(node2.right);
}
// 若node1的左节点为空,直接赋值
if (node1.left == null && node2.left != null) {
node1.left = node2.left;
}
// 若node1的右节点为空,直接赋值
if (node1.right == null && node2.right != null) {
node1.right = node2.right;
}
}
return root1;
}
}
3、700.二叉搜索树中的搜索
给定二叉搜索树(BST)的根节点 root
和一个整数值 val
。
你需要在 BST 中找到节点值等于 val
的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 null
。
思路: 因为是二叉搜索树,所以可以简化一般递归流程。
递归法
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
if (root == null || root.val == val) {
return root;
}
if (val > root.val) {
return searchBST(root.right, val);
} else {
return searchBST(root.left, val);
}
}
}
迭代法
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
while (root != null) {
if (root.val == val) {
return root;
} else if (root.val > val) {
root = root.left;
} else {
root = root.right;
}
}
return null; //没有找到
}
}
普通二叉树的解法
class Solution {
// 递归,普通二叉树
public TreeNode searchBST(TreeNode root, int val) {
if (root == null || root.val == val) {
return root;
}
TreeNode left = searchBST(root.left, val);
if (left != null) {
return left;
}
return searchBST(root.right, val);
}
}
class Solution {
// 迭代,普通二叉树
public TreeNode searchBST(TreeNode root, int val) {
if (root == null || root.val == val) {
return root;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode pop = stack.pop();
if (pop.val == val) {
return pop;
}
if (pop.right != null) {
stack.push(pop.right);
}
if (pop.left != null) {
stack.push(pop.left);
}
}
return null;
}
}
复习二叉搜索树(有序树)
- 若它的左子树不空,则左子树上所有节点的值均小于它的根节点的值;
- 若它的右子树不空,则右子树上所有节点的值均大于它的根节点的值;
- 它的左、右子树也分别为二叉搜索树。
4、98.验证二叉搜索树
给你一个二叉树的根节点 root
,判断其是否是一个有效的二叉搜索树。
有效 二叉搜索树定义如下:
- 节点的左子树只包含 小于 当前节点的数。
- 节点的右子树只包含 大于 当前节点的数。
- 所有左子树和右子树自身必须也是二叉搜索树。
思路: 可以递归中序遍历将二叉搜索树转变成一个数组。
递归法
class Solution {
TreeNode pre;
public boolean isValidBST(TreeNode root) {
if (root == null) {
return true;
}
// 左
boolean left = isValidBST(root.left);
if (!left) {
return false;
}
// 中
if (pre != null && root.val <= pre.val) {
return false;
}
pre = root;
// 右
boolean right = isValidBST(root.right);
return right;
}
}
迭代法
class Solution {
public boolean isValidBST(TreeNode root) {
if (root == null) {
return true;
}
TreeNode pre = null;
Stack<TreeNode> stack = new Stack<>();
while (root != null || !stack.isEmpty()) {
while (root != null) {
stack.push(root);
root = root.left;
}
TreeNode cur = stack.pop();
if (pre != null && cur.val <= pre.val) {
return false;
}
pre = cur;
root = cur.right;
}
return true;
}
}
注意
- 不能单纯的比较左节点小于中间节点,右节点大于中间节点就完事了。我们要比较的是左子树所有节点小于中间节点,右子树所有节点大于中间节点。
总结
最近实在是太忙了,想尽快学完java找个实习,然后还要上课和写作业,刷题时间就少很多了,每天唯一能保证的是知道今天的题目都怎么做,但整理博客就不一定了,有时候推到第二天也是难免的~