本文主要是介绍代码随想录-算法训练营day15【二叉树02:层序遍历、翻转二叉树、对称二叉树】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
代码随想录-035期-算法训练营【博客笔记汇总表】-CSDN博客
第六章 二叉树 part02今日内容: ● 层序遍历 10
● 226.翻转二叉树
● 101.对称二叉树 2 详细布置 层序遍历 看完本篇可以一口气刷十道题,试一试, 层序遍历并不难,大家可以很快刷了十道题。题目链接/文章讲解/视频讲解:https://programmercarl.com/0102.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E5%B1%82%E5%BA%8F%E9%81%8D%E5%8E%86.html226.翻转二叉树 (优先掌握递归) 这道题目 一些做过的同学 理解的也不够深入,建议大家先看我的视频讲解,无论做过没做过,都会有很大收获。题目链接/文章讲解/视频讲解:https://programmercarl.com/0226.%E7%BF%BB%E8%BD%AC%E4%BA%8C%E5%8F%89%E6%A0%91.html 101. 对称二叉树 (优先掌握递归) 先看视频讲解,会更容易一些。 题目链接/文章讲解/视频讲解:https://programmercarl.com/0101.%E5%AF%B9%E7%A7%B0%E4%BA%8C%E5%8F%89%E6%A0%91.html 往日任务
● day 1 任务以及具体安排:https://docs.qq.com/doc/DUG9UR2ZUc3BjRUdY
● day 2 任务以及具体安排:https://docs.qq.com/doc/DUGRwWXNOVEpyaVpG
● day 3 任务以及具体安排:https://docs.qq.com/doc/DUGdqYWNYeGhlaVR6
● day 4 任务以及具体安排:https://docs.qq.com/doc/DUFNjYUxYRHRVWklp
● day 5 周日休息
● day 6 任务以及具体安排:https://docs.qq.com/doc/DUEtFSGdreWRuR2p4
● day 7 任务以及具体安排:https://docs.qq.com/doc/DUElCb1NyTVpXa0Jj
● day 8 任务以及具体安排:https://docs.qq.com/doc/DUGdsY2JFaFhDRVZH
● day 9 任务以及具体安排:https://docs.qq.com/doc/DUHVXSnZNaXpVUHN4
● day 10 任务以及具体安排:https://docs.qq.com/doc/DUElqeHh3cndDbW1Q
●day 11 任务以及具体安排:https://docs.qq.com/doc/DUHh6UE5hUUZOZUd0
●day 12 周日休息
●day 13 任务以及具体安排:https://docs.qq.com/doc/DUHNpa3F4b2dMUWJ3
●day 14 任务以及具体安排:https://docs.qq.com/doc/DUHRtdXZZSWFkeGdE
目录
0102_层序遍历-10道题
0226_翻转二叉树
0101_对称二叉树2
0102_层序遍历-10道题
- Deque<TreeNode> deque = new LinkedList<>();
- Deque<TreeNode> deque = new ArrayList<>();
Deque<TreeNode> deque = new ArrayDeque<>();
队列使用这两个方法:
- deque.poll():方法用于从队列的开头(头部)移除并返回元素。如果队列为空,则返回
null
而不会抛出异常。- deque.offer():方法用于将元素添加到队列的末尾(尾部)。
/*** 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 List<List<Integer>> levelOrder(TreeNode root) {Deque<TreeNode> deque = new LinkedList<TreeNode>();if (root != null) {deque.push(root);//deque.offer(root);}List<List<Integer>> res = new ArrayList<List<Integer>>();while (!deque.isEmpty()) {int size = deque.size();ArrayList<Integer> tempList = new ArrayList<Integer>();while (size-- > 0) {//for (int i = 0; i < size; i++) {TreeNode treeNode = deque.poll();tempList.add(treeNode.val);if (treeNode.left != null) {deque.offer(treeNode.left);}if (treeNode.right != null) {deque.offer(treeNode.right);}}res.add(tempList);}return res;}
}
0226_翻转二叉树
import java.util.*;/*** 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 Solution0226 {public TreeNode invertTree(TreeNode root) {if (root == null) {return null;}Deque<TreeNode> deque = new LinkedList<TreeNode>();if (root != null) {deque.offer(root);}while (!deque.isEmpty()) {int size = deque.size();while (size-- > 0) {//for (int i = 0; i < size; i++) {}TreeNode treeNode = deque.poll();if (treeNode.left != null) {deque.offer(treeNode.left);}if (treeNode.right != null) {deque.offer(treeNode.right);}TreeNode temp = treeNode.left;treeNode.left = treeNode.right;treeNode.right = temp;}}return root;}
}class Solution0226_2 {//DFS递归/*** 前后序遍历都可以* 中序不行,因为先左孩子交换孩子,再根交换孩子(做完后,右孩子已经变成了原来的左孩子),再右孩子交换孩子(此时其实是对原来的左孩子做交换)*/public TreeNode invertTree(TreeNode root) {if (root == null) {return null;}invertTree(root.left);invertTree(root.right);swapChildren(root);return root;}private void swapChildren(TreeNode root) {TreeNode tmp = root.left;root.left = root.right;root.right = tmp;}
}class Solution0226_3 {//BFSpublic TreeNode invertTree(TreeNode root) {if (root == null) {return null;}ArrayDeque<TreeNode> deque = new ArrayDeque<>();deque.offer(root);while (!deque.isEmpty()) {int size = deque.size();while (size-- > 0) {TreeNode node = deque.poll();swap(node);if (node.left != null) deque.offer(node.left);if (node.right != null) deque.offer(node.right);}}return root;}public void swap(TreeNode root) {TreeNode temp = root.left;root.left = root.right;root.right = temp;}
}
0101_对称二叉树2
import java.util.*;/*** 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 Solution0101 {public boolean isSymmetric(TreeNode root) {}
}class Solution0101_2 {/*** 递归法*/public boolean isSymmetric1(TreeNode root) {return compare(root.left, root.right);}private boolean compare(TreeNode left, TreeNode right) {if (left == null && right != null) {return false;}if (left != null && right == null) {return false;}if (left == null && right == null) {return true;}if (left.val != right.val) {return false;}boolean compareOutside = compare(left.left, right.right);//比较外侧boolean compareInside = compare(left.right, right.left);//比较内侧return compareOutside && compareInside;}/*** 迭代法* 使用双端队列,相当于两个栈*/public boolean isSymmetric2(TreeNode root) {Deque<TreeNode> deque = new LinkedList<>();deque.offerFirst(root.left);deque.offerLast(root.right);while (!deque.isEmpty()) {TreeNode leftNode = deque.pollFirst();TreeNode rightNode = deque.pollLast();if (leftNode == null && rightNode == null) {continue;}
// if (leftNode == null && rightNode != null) {
// return false;
// }
// if (leftNode != null && rightNode == null) {
// return false;
// }
// if (leftNode.val != rightNode.val) {
// return false;
// }//以上三个判断条件合并if (leftNode == null || rightNode == null || leftNode.val != rightNode.val) {return false;}deque.offerFirst(leftNode.left);deque.offerFirst(leftNode.right);deque.offerLast(rightNode.right);deque.offerLast(rightNode.left);}return true;}/*** 迭代法* 使用普通队列*/public boolean isSymmetric3(TreeNode root) {Queue<TreeNode> deque = new LinkedList<>();deque.offer(root.left);deque.offer(root.right);while (!deque.isEmpty()) {TreeNode leftNode = deque.poll();TreeNode rightNode = deque.poll();if (leftNode == null && rightNode == null) {continue;}
// if (leftNode == null && rightNode != null) {
// return false;
// }
// if (leftNode != null && rightNode == null) {
// return false;
// }
// if (leftNode.val != rightNode.val) {
// return false;
// }//以上三个判断条件合并if (leftNode == null || rightNode == null || leftNode.val != rightNode.val) {return false;}//这里顺序与使用Deque不同deque.offer(leftNode.left);deque.offer(rightNode.right);deque.offer(leftNode.right);deque.offer(rightNode.left);}return true;}
}
这篇关于代码随想录-算法训练营day15【二叉树02:层序遍历、翻转二叉树、对称二叉树】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!