今日任务
226.翻转二叉树
101. 对称二叉树
104.二叉树的最大深度
111.二叉树的最小深度
226.翻转二叉树
题目连接 . - 力扣(LeetCode)
/**
* 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) {
Stack<TreeNode> stack = new Stack();
if (root == null) {return root;}
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.peek();
if (node != null) {
stack.pop();
if (node.right != null) { stack.push(node.right);}
if (node.left != null) { stack.push(node.left);}
stack.push(node);
stack.push(null);
} else {
stack.pop();
node = stack.peek();
stack.pop();
swap(node);
}
}
return root;
}
public void swap(TreeNode root){
TreeNode tmp = root.left;
root.left = root.right;
root.right = tmp;
return;
}
}
101. 对称二叉树
题目连接 . - 力扣(LeetCode)
/**
* 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> deque = new LinkedList<>();
deque.offerFirst(root.left);
deque.offerFirst(root.right);
while (!deque.isEmpty()) {
TreeNode leftNode = deque.pollFirst();
TreeNode rightNode = deque.pollLast();
if (leftNode == null && rightNode == null) {
continue;
}
if (leftNode == null || rightNode == null || leftNode.val != rightNode.val){
return false;
}
deque.offerFirst(leftNode.left);
deque.offerFirst(leftNode.right);
deque.offerLast(rightNode.right);
deque.offerLast(rightNode.left);
}
return true;
}
}
104.二叉树的最大深度
题目连接 . - 力扣(LeetCode)
/**
* 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;
Queue<TreeNode> que = new LinkedList<>();
que.offer(root);
int dep = 0;
while (!que.isEmpty()) {
dep++;
int size = que.size();
while (size > 0) {
if (que.peek().left != null) {que.offer(que.peek().left);}
if (que.peek().right != null) {que.offer(que.peek().right);}
que.poll();
size--;
}
}
return dep;
}
}
111.二叉树的最小深度
题目连接 . - 力扣(LeetCode)
/**
* 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) {
if(root == null) return 0;
Queue<TreeNode> que = new LinkedList<>();
que.offer(root);
int dep = 0;
while (!que.isEmpty()) {
dep++;
int size = que.size();
while (size > 0) {
if (que.peek().left != null) {que.offer(que.peek().left);}
if (que.peek().right != null) {que.offer(que.peek().right);}
if (que.peek().right == null && que.peek().left == null) {return dep;}
que.poll();
size--;
}
}
return dep;
}
}