LeetCode [102. 二叉树的层序遍历]
题目:给你二叉树的根节点 root
,返回其节点值的 层序遍历 。 (即逐层地,从左到右访问所有节点)。
示例 1:
输入:root = [3,9,20,null,null,15,7]
输出:[[3],[9,20],[15,7]]
示例 2:
输入:root = [1]
输出:[[1]]
示例 3:
输入:root = []
输出:[]
提示:
- 树中节点数目在范围
[0, 2000]
内 -1000 <= Node.val <= 1000
思路:
//借助队列
/**
* 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>> res = new ArrayList<List<Integer>>();
if(root == null){
return res;
}
Queue<TreeNode> queue = new LinkedList<TreeNode>();
queue.offer(root);
while(!queue.isEmpty()){
List<Integer> level = new ArrayList<Integer>();
int currentLevelSize = queue.size();
for(int i = 1;i <= currentLevelSize;i++){
TreeNode node = queue.poll();
level.add(node.val);
if(node.left != null){
queue.offer(node.left);
}
if(node.right != null){
queue.offer(node.right);
}
}
res.add(level);
}
return res;
}
}
LeetCode [226. 翻转二叉树]
题目:给你一棵二叉树的根节点 root
,翻转这棵二叉树,并返回其根节点。
示例 1:
输入:root = [4,2,7,1,3,6,9]
输出:[4,7,2,9,6,3,1]
示例 2:
输入:root = [2,1,3]
输出:[2,3,1]
示例 3:
输入:root = []
输出:[]
提示:
- 树中节点数目范围在
[0, 100]
内 -100 <= Node.val <= 100
思路:
//递归法
/**
* 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 null;
}
invertTree(root.left);
invertTree(root.right);
swap(root);
return root;
}
public void swap(TreeNode root){
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
}
}
//迭代法(层序遍历)
/**
* 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) {
//将二叉树中的节点逐层放入队列中,再迭代处理队列中的元素
Deque<TreeNode> res = new ArrayDeque<>();
if(root == null){
return null;
}
res.offer(root);
while(!res.isEmpty()){
int currentLevelSize = res.size();
for(int i = 0;i < currentLevelSize;i++){
//每次都从队列中拿一个节点,并交换这个节点的左右子树
TreeNode node = res.poll();
swap(node);
//如果当前节点的左子树不为空,则放入队列等待后续处理
if(node.left != null){
res.offer(node.left);
}
//如果当前节点的右子树不为空,则放入队列等待后续处理
if(node.right != null){
res.offer(node.right);
}
}
}
//返回处理完的根节点
return root;
}
//交换当前节点的左右孩子
public void swap(TreeNode root){
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
}
}
LeetCode [101. 对称二叉树]
题目:给你一个二叉树的根节点 root
, 检查它是否轴对称。
示例 1:
输入:root = [1,2,2,3,4,4,3]
输出:true
示例 2:
输入:root = [1,2,2,null,3,null,3]
输出:false
提示:
- 树中节点数目在范围
[1, 1000]
内 -100 <= Node.val <= 100
思路:
//递归法
/**
* 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);
}
public boolean compare(TreeNode leftNode,TreeNode rightNode){
if(leftNode != null && rightNode ==null){
return false;
}
if(leftNode == null && rightNode !=null){
return false;
}
if(leftNode == null && rightNode ==null){
return true;
}
if(leftNode.val != rightNode.val){
return false;
}
boolean compareOut = compare(leftNode.left,rightNode.right);
boolean compareIn = compare(leftNode.right,rightNode.left);
return compareOut && compareIn;
}
}
//迭代法
/**
* 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) {
Deque<TreeNode> res = new LinkedList<>();
res.offerLast(root.left);
res.offerFirst(root.right);
while(!res.isEmpty()){
TreeNode leftNode = res.pollLast();
TreeNode rightNode = res.pollFirst();
if(leftNode == null && rightNode == null){
continue;
}
if(leftNode == null || rightNode == null || leftNode.val != rightNode.val){
return false;
}
res.offerLast(leftNode.left);
res.offerLast(leftNode.right);
res.offerFirst(rightNode.right);
res.offerFirst(rightNode.left);
}
return true;
}
}