目录
LeetCode 102.二叉树的层序遍历
文章讲解:代码随想录
视频讲解:讲透二叉树的层序遍历 | 广度优先搜索 | LeetCode:102.二叉树的层序遍历_哔哩哔哩_bilibili
力扣题目:LeetCode 102. 二叉树的层序遍历
题目分析:
首先先分析返回的参数类型为List<List<Integer>>, 设置相应的类型变量。其次,关注三个关键点。第一个为要用队列的数据结构来存储二叉树的结点,为什么要用队列呢?因为层序遍历符合先进先出的特点。第二个关键点为如何进入二叉树的每一层,这里就需要对队列进行特殊处理。我们每添加一个结点进去,就要判断这个结点是否有左右结点,有则添加,没有就继续循环。此外,还需要一个变量size来记录每一层的个数。第三个关键点就是如何判断二叉树层序遍历完成,当我们的队列为空的时候,即为二叉树的层序遍历完成。
代码如下(Java):
/**
* 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) {
//作为接受结果的的变量
List<List<Integer>> resList = new ArrayList<List<Integer>>();
//先判断二叉树的根节点进行判断
if(root == null) return resList;
//创建队列,队列的元素为TreeNode
Queue<TreeNode> queue = new LinkedList<TreeNode>();
//根据上面代码已经判断根节点为非空,所以首先添加根节点进入队列
queue.offer(root);
//队列不为空的情况下一直循环
while(!queue.isEmpty()){
//记录每一层的个数,做层数判断
int size = queue.size();
//把每一层结点的val值,放入tmpList当中
List<Integer> tmpList = new ArrayList<Integer>();
//做每一个层的结点遍历
while(size-- > 0){
//临时变量tmpNode来存储队列弹出的结点
TreeNode tmpNode = queue.poll();
//再把这个结点的值添加到tmpList
tmpList.add(tmpNode.val);
//对队列弹出的结点做其左右结点添加的操作
if(tmpNode.left != null) queue.offer(tmpNode.left);
if(tmpNode.right != null) queue.offer(tmpNode.right);
}
//最后把每一层tmpList添加到最后的resList当中
resList.add(tmpList);
}
//返回结果集
return resList;
}
}
LeetCode 226.翻转二叉树
文章讲解:代码随想录
视频讲解:听说一位巨佬面Google被拒了,因为没写出翻转二叉树 | LeetCode:226.翻转二叉树_哔哩哔哩_bilibili
力扣题目:LeetCode 226. 翻转二叉树
题目分析:
翻转二叉树实际上就是翻转每一个结点的左右子树。遍历二叉树有两种方法:一种就是递归(递归又分为前序遍历,中序遍历,后序遍历);一种就是层序遍历的方法。
代码如下(Java):前序遍历
/**
* 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 TreeNode invertTree(TreeNode root) {
if(root == null) return root;
swap(root);
invertTree(root.left);
invertTree(root.right);
return root;
}
private void swap(TreeNode node){
TreeNode tmpNode = node.left;
node.left = node.right;
node.right = tmpNode;
}
}
代码如下(Java):中序遍历
注意:中序遍历的时候,因为他是在中间的时候就进行了交换,所以想要翻转原来的右子树,就得翻转交换后的左子树。
/**
* 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 TreeNode invertTree(TreeNode root) {
if(root == null) return root;
invertTree(root.left);
swap(root);
invertTree(root.left);
return root;
}
private void swap(TreeNode node){
TreeNode tmpNode = node.left;
node.left = node.right;
node.right = tmpNode;
}
}
代码如下(Java):后序遍历
/**
* 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 TreeNode invertTree(TreeNode root) {
if(root == null) return root;
invertTree(root.left);
invertTree(root.right);
swap(root);
return root;
}
private void swap(TreeNode node){
TreeNode tmpNode = node.left;
node.left = node.right;
node.right = tmpNode;
}
}
代码如下(Java):层序遍历
/**
* 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 TreeNode invertTree(TreeNode root) {
if(root == null) return root;
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
while(!queue.isEmpty()){
int size = queue.size();
while(size-- > 0){
TreeNode tmpNode = queue.poll();
swap(tmpNode);
if(tmpNode.left != null) queue.offer(tmpNode.left);
if(tmpNode.right != null) queue.offer(tmpNode.right);
}
}
return root;
}
private void swap(TreeNode node){
TreeNode tmpNode = node.left;
node.left = node.right;
node.right = tmpNode;
}
}
LeetCode 101.对称二叉树
文章讲解:代码随想录
视频讲解:新学期要从学习二叉树开始! | LeetCode:101. 对称二叉树_哔哩哔哩_bilibili
力扣题目:LeetCode 101.对称二叉树
题目讲解:这题目可以看作是结点的左子树翻转是否等于右子树。
代码如下(Java):
/**
* 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 boolean isSymmetric(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 compareInside && compareOutside;
}
}