代码随想录算法训练营第二十天 |654.最大二叉树,617.合并二叉树,700.二叉搜索树种的搜索,98.验证二叉搜索树(待补充)

缺乏、安全感 2024-04-23 04:06 124阅读 0赞

654.最大二叉树

1、题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

2、文章讲解:代码随想录

3、题目:

给定一个不含重复元素的整数数组。一个以此数组构建的最大二叉树定义如下:

  • 二叉树的根是数组中的最大元素。
  • 左子树是通过数组中最大值左边部分构造出的最大二叉树。
  • 右子树是通过数组中最大值右边部分构造出的最大二叉树。

通过给定的数组构建最大二叉树,并且输出这个树的根节点。

示例 :

3a19e0fccb38d2e623a0f65b4ed51457.png

提示:

给定的数组的大小在 [1, 1000] 之间。

4、视频链接:

又是构造二叉树,又有很多坑!| LeetCode:654.最大二叉树_哔哩哔哩_bilibili

5、思路:

  1. class Solution {
  2. public TreeNode constructMaximumBinaryTree(int[] nums) {
  3. return constructMaximumBinaryTree1(nums, 0, nums.length);
  4. }
  5. public TreeNode constructMaximumBinaryTree1(int[] nums, int leftIndex, int rightIndex) {
  6. if (rightIndex - leftIndex < 1) {// 没有元素了
  7. return null;
  8. }
  9. if (rightIndex - leftIndex == 1) {// 只有一个元素
  10. return new TreeNode(nums[leftIndex]);
  11. }
  12. int maxIndex = leftIndex;// 最大值所在位置
  13. int maxVal = nums[maxIndex];// 最大值
  14. for (int i = leftIndex + 1; i < rightIndex; i++) {
  15. if (nums[i] > maxVal){
  16. maxVal = nums[i];
  17. maxIndex = i;
  18. }
  19. }
  20. TreeNode root = new TreeNode(maxVal);
  21. // 根据maxIndex划分左右子树
  22. root.left = constructMaximumBinaryTree1(nums, leftIndex, maxIndex);
  23. root.right = constructMaximumBinaryTree1(nums, maxIndex + 1, rightIndex);
  24. return root;
  25. }
  26. }
  27. class Solution {
  28. public int findBottomLeftValue(TreeNode root) {
  29. Queue<TreeNode> queue = new LinkedList<>();
  30. queue.offer(root);
  31. int res = 0;
  32. while (!queue.isEmpty()) {
  33. int size = queue.size();
  34. for (int i = 0; i < size; i++) {
  35. TreeNode poll = queue.poll();
  36. if (i == 0) {
  37. res = poll.val;
  38. }
  39. if (poll.left != null) {
  40. queue.offer(poll.left);
  41. }
  42. if (poll.right != null) {
  43. queue.offer(poll.right);
  44. }
  45. }
  46. }
  47. return res;
  48. }
  49. }

617.合并二叉树

1、题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

2、文章讲解:代码随想录

3、题目:

给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠。

你需要将他们合并为一个新的二叉树。合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新值,否则不为 NULL 的节点将直接作为新二叉树的节点。

示例 1:

09a8627257d342e2ad66bd2a216f78c4.png

注意: 合并必须从两个树的根节点开始。

4、视频链接:

一起操作两个二叉树?有点懵!| LeetCode:617.合并二叉树_哔哩哔哩_bilibili

5、思路:

  1. class Solution {
  2. // 递归
  3. public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
  4. if (root1 == null) return root2;
  5. if (root2 == null) return root1;
  6. root1.val += root2.val;
  7. root1.left = mergeTrees(root1.left,root2.left);
  8. root1.right = mergeTrees(root1.right,root2.right);
  9. return root1;
  10. }
  11. }
  12. class Solution {
  13. // 使用栈迭代
  14. public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
  15. if (root1 == null) {
  16. return root2;
  17. }
  18. if (root2 == null) {
  19. return root1;
  20. }
  21. Stack<TreeNode> stack = new Stack<>();
  22. stack.push(root2);
  23. stack.push(root1);
  24. while (!stack.isEmpty()) {
  25. TreeNode node1 = stack.pop();
  26. TreeNode node2 = stack.pop();
  27. node1.val += node2.val;
  28. if (node2.right != null && node1.right != null) {
  29. stack.push(node2.right);
  30. stack.push(node1.right);
  31. } else {
  32. if (node1.right == null) {
  33. node1.right = node2.right;
  34. }
  35. }
  36. if (node2.left != null && node1.left != null) {
  37. stack.push(node2.left);
  38. stack.push(node1.left);
  39. } else {
  40. if (node1.left == null) {
  41. node1.left = node2.left;
  42. }
  43. }
  44. }
  45. return root1;
  46. }
  47. }

700.二叉搜索树中的搜索

1、题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

2、文章讲解:代码随想录

3、题目:

给定二叉搜索树(BST)的根节点和一个值。 你需要在BST中找到节点值等于给定值的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 NULL。

例如,

ce0321fcb81e3b550c42bc66ccce4e35.png

在上述示例中,如果要找的值是 5,但因为没有节点值为 5,我们应该返回 NULL。

4、思路:

  1. if (root == null || root.val == val) {
  2. return root;
  3. }
  4. TreeNode left = searchBST(root.left, val);
  5. if (left != null) {
  6. return left;
  7. }
  8. return searchBST(root.right, val);

98.验证二叉搜索树

1、题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

2、文章讲解:代码随想录

3、题目:

给定一个二叉树,判断其是否是一个有效的二叉搜索树。

假设一个二叉搜索树具有如下特征:

  • 节点的左子树只包含小于当前节点的数。
  • 节点的右子树只包含大于当前节点的数。
  • 所有左子树和右子树自身必须也是二叉搜索树。

95e0d3f9c2210592fb82e933a0499e45.png

4、思路:

  1. class Solution {
  2. // 递归
  3. TreeNode max;
  4. public boolean isValidBST(TreeNode root) {
  5. if (root == null) {
  6. return true;
  7. }
  8. // 左
  9. boolean left = isValidBST(root.left);
  10. if (!left) {
  11. return false;
  12. }
  13. // 中
  14. if (max != null && root.val <= max.val) {
  15. return false;
  16. }
  17. max = root;
  18. // 右
  19. boolean right = isValidBST(root.right);
  20. return right;
  21. }
  22. }

发表评论

表情:
评论列表 (有 0 条评论,124人围观)

还没有评论,来说两句吧...

相关阅读