💡 解题思路
- 📝 确定输入与输出
- 🔍 分析复杂度
- 🔨 复杂题目拆分 :严谨且完整 地拆分为更小的可以解决的子问题(根据二叉树的不同遍历方式 + 过程处理)–(多总结)
- 💭 选择处理逻辑: 根据拆分后的子问题,总结并选择合适的问题处理思路(前,中,后序,层序遍历)
- 🔎 检查特殊情况:边界条件和特殊情况(递归返回值、递归顺序)
- 🏁 返回结果
226.翻转二叉树
一、递归
class Solution {
public TreeNode mirrorTree(TreeNode root) {
if (root == null) return null;
TreeNode left = mirrorTree(root.left);
TreeNode right = mirrorTree(root.right);
// 后续处理翻转
root.right = left;
root.left = right;
return root;
}
}
二、迭代
class Solution {
public TreeNode mirrorTree(TreeNode root) {
if (root == null) return null;
Stack<TreeNode> stack = new Stack<>();
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;
}
}
101. 对称二叉树
一、递归
class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null) return true;
return reverse(root.left, root.right);
}
private boolean reverse(TreeNode left, TreeNode right){
if (left == null && right == null) return true;
else if (left == null || right == null) return false;
else if (left.val != right.val) return false;
else {
return reverse(left.left, right.right) && reverse(left.right, right.left);
}
}
}
二、迭代
class Solution {
public boolean isSymmetric(TreeNode root) {
if (root == null) return true;
Stack<TreeNode> stack = new Stack<>();
// 注意节点加入顺序
stack.push(root.left);
stack.push(root.right);
while(!stack.isEmpty()) {
TreeNode right = stack.pop();
TreeNode left = stack.pop();
if (left == null && right == null) continue;
else if (left == null || right == null) return false;
else if (left.val != right.val) return false;
else {
stack.push(left.left);
stack.push(right.right);
stack.push(left.right);
stack.push(right.left);
}
}
return true;
}
}
104.二叉树的最大深度
一、递归
class Solution {
public int maxDepth(TreeNode root) {
if (root == null) return 0;
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}
}
二、迭代
class Solution {
// 层序遍历,统计
public int maxDepth(TreeNode root) {
int res = 0;
if (root == null) return res;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()) {
int size = queue.size();
while(size > 0) {
size--;
TreeNode node = queue.poll();
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
res++;
}
return res;
}
}
一、递归
class Solution {
public int minDepth(TreeNode root) {
if (root == null) return 0;
if (root.left == null && root.right == null) return 1; // 到叶子节点
if (root.left == null) return minDepth(root.right)+1;
if (root.right == null) return minDepth(root.left)+1;
return Math.min(minDepth(root.left), minDepth(root.right)) +1;
}
}
二、迭代
class Solution {
public int minDepth(TreeNode root) {
int res = 0;
if (root == null) return res;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()) {
int size = queue.size();
res++;
while(size > 0) {
size--;
TreeNode node = queue.poll();
if (node.left == null && node.right == null) return res;
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
}
return res;
}
}