给你二叉树的根节点 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<>();
if (root == null) {
return res;
}
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
List<Integer> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
list.add(node.val);
if (node.left!= null) {
queue.offer(node.left);
}
if (node.right!= null) {
queue.offer(node.right);
}
}
res.add(list);
}
return res;
}
}
class Solution {
List<List<Integer>> res = new ArrayList<>();
public List<List<Integer>> levelOrder(TreeNode root) {
level(root, 1);
return res;
}
public void level(TreeNode root, int deepth){
if(root == null) return ;
//返回列表中的元素个数==层级数
if(res.size() < deepth){
List<Integer> item = new ArrayList<Integer>();
res.add(item);
}
res.get(deepth-1).add(root.val);
//左右节点位于同一层级deepth+1
level(root.left, deepth+1);
level(root.right, deepth+1);
}
}
给你一棵二叉树的根节点 root
,翻转这棵二叉树,并返回其根节点。
输入: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) {
//终止条件 节点为null
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 null;
}
Stack<TreeNode> stack = new Stack<TreeNode>();
stack.push(root);
while(!stack.isEmpty()) {
TreeNode node = stack.pop();
if(node.right!= null) {
stack.push(node.right);
}
if(node.left!= null) {
stack.push(node.left);
}
TreeNode temp = node.left;
node.left = node.right;
node.right = temp;
}
return root;
}
}
给你一个二叉树的根节点 root
, 检查它是否轴对称。
输入:root = [1,2,2,3,4,4,3] 输出:true
输入: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 checkLeftEquelsRight(root.left, root.right);
}
public boolean checkLeftEquelsRight(TreeNode left, TreeNode right) {
if(left ==null && right ==null){
return true;
}
if(right!=null && left == null){
return false;
}
if(left!=null && right==null){
return false;
}
if(left.val != right.val) {
return false;
}
return checkLeftEquelsRight(left.left,right.right) && checkLeftEquelsRight(left.right,right.left);
}
}