提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
/**
* 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<>();
Deque<TreeNode> deq = new ArrayDeque<>();
List<Integer> li = new ArrayList<>();
if(root == null)return res;
deq.offerLast(root);
TreeNode pre = root;
while(!deq.isEmpty()){
TreeNode p = deq.pollFirst();
li.add(p.val);
if(p.left != null)deq.offerLast(p.left);
if(p.right != null)deq.offerLast(p.right);
if(pre == p){
res.add(li);
li = new ArrayList();
pre = deq.peekLast();
}
}
return res;
}
}
/**
* 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>> levelOrderBottom(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
Deque<TreeNode> deq = new ArrayDeque<>();
if(root == null)return res;
deq.offerLast(root);
while(!deq.isEmpty()){
int len = deq.size();
List<Integer> li = new ArrayList<>();
for(int i = 0; i < len; i ++){
TreeNode p = deq.pollFirst();
li.add(p.val);
if(p.left != null)deq.offerLast(p.left);
if(p.right != null)deq.offerLast(p.right);
}
res.add(li);
}
List<List<Integer>> r = new ArrayList<>();
for(int i = res.size()-1; i >= 0; i--){
r.add(res.get(i));
}
return r;
}
}
/**
* 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<Integer> rightSideView(TreeNode root) {
List<Integer> res = new ArrayList<>();
Deque<TreeNode> deq = new ArrayDeque<>();
if(root == null)return res;
deq.offerLast(root);
while(!deq.isEmpty()){
int len = deq.size();
for(int i = 0; i < len; i ++){
TreeNode p = deq.pollFirst();
if(i == len - 1){
res.add(p.val);
}
if(p.left != null)deq.offerLast(p.left);
if(p.right != null)deq.offerLast(p.right);
}
}
return res;
}
}
/**
* 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<Double> averageOfLevels(TreeNode root) {
List<Double> res = new ArrayList<>();
Deque<TreeNode> deq = new ArrayDeque<>();
deq.offerLast(root);
while(!deq.isEmpty()){
int len = deq.size();
double count = 0;
for(int i = 0; i < len; i ++){
TreeNode p = deq.pollFirst();
count += p.val;
if(p.left != null)deq.offerLast(p.left);
if(p.right != null)deq.offerLast(p.right);
}
res.add(count / len);
}
return res;
}
}
/*
// Definition for a Node.
class Node {
public int val;
public List children;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, List _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public List<List<Integer>> levelOrder(Node root) {
List<List<Integer>> res = new ArrayList<>();
Deque<Node> deq = new ArrayDeque<>();
if(root == null)return res;
deq.offerLast(root);
while(!deq.isEmpty()){
int len = deq.size();
List<Integer> l = new ArrayList<>();
for(int i = 0; i < len; i ++){
Node p = deq.pollFirst();
l.add(p.val);
List<Node> li = p.children;
for(Node n : li){
if(n != null)deq.offerLast(n);
}
}
res.add(l);
}
return res;
}
}
/**
* 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<Integer> largestValues(TreeNode root) {
List<Integer> res = new ArrayList<>();
Deque<TreeNode> deq = new ArrayDeque<>();
if(root == null)return res;
deq.offerLast(root);
while(!deq.isEmpty()){
int len = deq.size();
int max = Integer.MIN_VALUE;
for(int i = 0; i < len; i ++){
TreeNode p = deq.pollFirst();
max = max > p.val ? max:p.val;
if(p.left != null)deq.offerLast(p.left);
if(p.right != null)deq.offerLast(p.right);
}
res.add(max);
}
return res;
}
}
/*
// Definition for a Node.
class Node {
public int val;
public Node left;
public Node right;
public Node next;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, Node _left, Node _right, Node _next) {
val = _val;
left = _left;
right = _right;
next = _next;
}
};
*/
class Solution {
public Node connect(Node root) {
Deque<Node> deq = new ArrayDeque<>();
if(root == null)return root;
deq.offerLast(root);
while(!deq.isEmpty()){
int len = deq.size();
for(int i = 0; i < len; i ++){
Node p = deq.pollFirst();
if(i != len-1){
Node ne = deq.peekFirst();
p.next = ne;
}
if(p.left != null)deq.offerLast(p.left);
if(p.right != null)deq.offerLast(p.right);
}
}
return root;
}
}
/*
// Definition for a Node.
class Node {
public int val;
public Node left;
public Node right;
public Node next;
public Node() {}
public Node(int _val) {
val = _val;
}
public Node(int _val, Node _left, Node _right, Node _next) {
val = _val;
left = _left;
right = _right;
next = _next;
}
};
*/
class Solution {
public Node connect(Node root) {
Deque<Node> deq = new ArrayDeque<>();
if(root == null)return root;
deq.offerLast(root);
while(!deq.isEmpty()){
int len = deq.size();
for(int i = 0; i < len; i ++){
Node p = deq.pollFirst();
if(i != len-1){
Node ne = deq.peekFirst();
p.next = ne;
}
if(p.left != null)deq.offerLast(p.left);
if(p.right != null)deq.offerLast(p.right);
}
}
return 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 int maxDepth(TreeNode root) {
if(root == null){
return 0;
}
int l = maxDepth(root.left);
int r = maxDepth(root.right);
return l > r ? l + 1: r + 1;
}
}
迭代
/**
* 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 int maxDepth(TreeNode root) {
Deque<TreeNode> deq = new ArrayDeque<>();
if(root == null)return 0;
deq.offerLast(root);
int high = 0;
while(!deq.isEmpty()){
int len = deq.size();
for(int i = 0; i < len; i ++){
TreeNode p = deq.pollFirst();
if(p.left != null)deq.offerLast(p.left);
if(p.right != null)deq.offerLast(p.right);
}
high ++;
}
return high;
}
}
/**
* 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 int minDepth(TreeNode root) {
Deque<TreeNode> deq = new ArrayDeque<>();
if(root == null)return 0;
deq.offerLast(root);
int high = 0;
while(!deq.isEmpty()){
int len = deq.size();
for(int i = 0; i < len; i ++){
TreeNode p = deq.pollFirst();
if(p.left == null && p.right == null){
return high + 1;
}
if(p.left != null)deq.offerLast(p.left);
if(p.right != null)deq.offerLast(p.right);
}
high ++;
}
return high;
}
}
迭代
/**
* 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> deq = new ArrayDeque<>();
if(root == null)return root;
deq.offerLast(root);
while(!deq.isEmpty()){
int len = deq.size();
for(int i = 0; i < len; i ++){
TreeNode p = deq.pollFirst();
TreeNode temp = p.left;
p.left = p.right;
p.right = temp;
if(p.left != null)deq.offerLast(p.left);
if(p.right != null)deq.offerLast(p.right);
}
}
return 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 null;
TreeNode chirlLeft = invertTree(root.left);
TreeNode chirlRight = invertTree(root.right);
TreeNode temp = chirlLeft;
root.left = chirlRight;
root.right = temp;
return 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) {
Deque<TreeNode> deq = new ArrayDeque<>();
if(root == null)return true;
if(root.left == null && root.right == null)return true;
if(root.left == null)return false;
if(root.right == null)return false;
if(root.left.val != root.right.val)return false;
deq.offerLast(root.left);
deq.offerLast(root.right);
while(!deq.isEmpty()){
int len = deq.size();
if(len % 2 == 1)return false;
TreeNode[] arr = new TreeNode[len];
for(int i = 0; i < len; i ++){
TreeNode p = deq.pollFirst();
arr[i] = p;
if(p.left != null)deq.offerLast(p.left);
if(p.right != null)deq.offerLast(p.right);
}
for(int i = 0, j = len-1; i < j ; i ++, j --){
if(arr[i].left == null && arr[j].right != null)return false;
if(arr[i].left != null && arr[j].right == null)return false;
if(arr[j].left == null && arr[i].right != null)return false;
if(arr[j].left != null && arr[i].right == null)return false;
if(arr[i].left != null && arr[j].right != null && arr[i].left.val !=
arr[j].right.val)return false;
if(arr[j].left != null && arr[i].right != null && arr[j].left.val !=
arr[i].right.val)return false;
}
}
return true;
}
}
迭代版本二
/**
* 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> deq = new LinkedList<>();
if(root == null)return true;
deq.offerLast(root.left);
deq.offerLast(root.right);
while(!deq.isEmpty()){
TreeNode childLeft = deq.pollFirst();
TreeNode childRight = deq.pollFirst();
if(childLeft == null && childRight == null)continue;
if(childLeft != null && childRight == null)return false;
if(childLeft == null && childRight != null)return false;
if(childLeft != null && childRight != null && childLeft.val != childRight.val)return false;
deq.offerLast(childLeft.left);
deq.offerLast(childRight.right);
deq.offerLast(childLeft.right);
deq.offerLast(childRight.left);
}
return true;
}
}
递归
/**
* 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 compareTree(root.left, root.right);
}
public boolean compareTree(TreeNode childL, TreeNode childR){
if(childL == null && childR != null)return false;
if(childL != null && childR == null)return false;
if(childL == null && childR == null)return true;
if(childL.val != childR.val)return false;
boolean boolL = compareTree(childL.left, childR.right);
boolean boooR = compareTree(childL.right, childR.left);
return boolL && boooR;
}
}