102.二叉树的层序遍历
题目链接:https://leetcode.cn/problems/binary-tree-level-order-traversal/
题目要求:给你二叉树的根节点 root
,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)。
🤔解题思路
利用队列存储每层的节点,再根据这层的节点遍历下一层的节点
/**
* 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>> lists = new ArrayList();
Queue<TreeNode> queue = new LinkedList();
if(root != null) queue.add(root);
while(!queue.isEmpty()){
int size = queue.size(); //存储每一层的节点数
List<Integer> list = new ArrayList();
//遍历该层节点,并将下一层的子节点加入到队列中
while(size > 0){
TreeNode temp = queue.poll();
list.add(temp.val);
if(temp.left != null) queue.add(temp.left);
if(temp.right != null) queue.add(temp.right);
size--;
}
//将该层的元素加入结果集
lists.add(list);
}
return lists;
}
}
226.翻转二叉树
题目链接:https://leetcode.cn/problems/invert-binary-tree/submissions/
题目要求:给你一棵二叉树的根节点 root
,翻转这棵二叉树,并返回其根节点。
🤔解题思路
在递归遍历中,采用前后序遍历会更好理解一点,遍历到中间节点的时候交换左右子节点;
中序遍历容易出错,因为先遍历左节点后遍历中间节点,遍历右节点的时候,右节点就是之前遍历后交换过来的左节点,所以遍历完左节点和中间节点后要再遍历左节点!
/**
* 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;
//,遍历中间节点,交换左右子节点
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
//遍历左节点
invertTree(root.left);
//遍历右节点
invertTree(root.right);
return root;
}
}
//后序遍历
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root == null) return root;
invertTree(root.left);
invertTree(root.right);
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
return root;
}
}
//中序遍历
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root == null) return root;
invertTree(root.left);
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
invertTree(root.left);
return root;
}
}
101.对称二叉树
题目链接:https://leetcode.cn/problems/symmetric-tree/submissions/
题目要求:给你一个二叉树的根节点 root
, 检查它是否轴对称。
🤔解题思路
只能用后序遍历,因为遍历时需要返回左右节点的信息给中间节点;相当于验证root节点的左子树是否和右子树对称,及判断左子树内节点是否等于右子树外节点,左子树外节点是否等于右子树内节点,若相等则为对称
/**
* 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) {
if(root == null) return true;
return compare(root.left,root.right);
}
public boolean compare(TreeNode left,TreeNode right){
if(left == null && right != null) return false;
else if(left != null && right == null) return false;
else if(left == null && right == null) return true;
else if(left.val != right.val) return false;
return compare(left.left,right.right) && compare(left.right,right.left);
}
}