1. LeetCode 226.翻转二叉树
题目链接:https://leetcode.cn/problems/invert-binary-tree/description/
文章链接:https://programmercarl.com/0226.翻转二叉树.html
视频链接:https://www.bilibili.com/video/BV1sP4y1f7q7
思路:
不能使用递归的中序遍历来解决,可以使用递归的前后序遍历或层序遍历来解决。
解法:
/**
* 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) {
find(root);
return root;
}
public void find(TreeNode cur) {// 前序遍历
if (cur == null) {
return;
}
TreeNode temp = cur.left;
cur.left = cur.right;
cur.right = temp;
find(cur.left);
find(cur.right);
}
}
2. LeetCode 101. 对称二叉树
题目链接:https://leetcode.cn/problems/symmetric-tree/description/
文章链接:https://programmercarl.com/0101.对称二叉树.html
视频链接:https://www.bilibili.com/video/BV1ue4y1Y7Mf
思路:
因为要关注左右两棵树是否对称,所以使用递归时,入参应该书左右两个节点。
对单层进行终止条件判断:
1、左节点为空,但右节点不为空,返回false
2、左节点不为空,但右节点为空,返回false
3、左右节点都为空,此时返回true
4、左右节点都不为空,但两节点值不相同,返回false
单层递归逻辑:
在获取左右节点都不为空且值相同后,就要对下一层进行判断。
比较外侧,即左节点的左节点和右节点的右节点
比较内侧,即左节点的右节点和右节点的左节点
两侧返回的结果的并,表示当前层的最终判断结果。
解法:
/**
* 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) {
if (root == null) return false;
return compare(root.left,root.right);
}
public boolean compare(TreeNode left,TreeNode right) {
// 设置终止条件
if (left == null && right != null) {
return false;
} else if (left != null && right == null) {
return false;
} else if (left == null && right == null) {
return true;
} else if (left.val != right.val) {
return false;
}
// 单层递归逻辑 (后序遍历)
boolean leftTree = compare(left.left,right.right);
boolean rightTree = compare(left.right,right.left);
return leftTree && rightTree;
}
}
3. LeetCode 104.二叉树的最大深度
题目链接:https://leetcode.cn/problems/maximum-depth-of-binary-tree/
文章链接:https://programmercarl.com/0104.二叉树的最大深度.html
视频链接:https://www.bilibili.com/video/BV1Gd4y1V75u
思路:
本题可以使用前序(中左右),也可以使用后序遍历(左右中),使用前序求的就是深度,使用后序求的是高度。
二叉树节点的深度:指从任意节点到根节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始)
二叉树节点的高度:指从任意节点到叶子节点的最长简单路径边的条数或者节点数(取决于高度从0开始还是从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 {
// int deep = 0;
// public int maxDepth(TreeNode root) {
// find(root,deep);
// return deep;
// }
// // 前序遍历
// public void find(TreeNode cur,int curDeep){
// if (cur == null) {
// return;
// }
// curDeep++;
// if (cur.left == null && cur.right == null) {
// if (curDeep > deep) {
// deep = curDeep;
// }
// }
// find(cur.left,curDeep);
// find(cur.right,curDeep);
// }
public int maxDepth(TreeNode root) {
int res = height(root);// 计算根节点的高度,也是最大深度
return res;
}
// 后序遍历
public int height(TreeNode cur) {
if (cur == null) return 0;
int leftHeight = height(cur.left);// 左子树的高度
int rightHeight = height(cur.right);// 右子树的高度
return 1+Math.max(leftHeight,rightHeight);// 当前节点的高度
}
}
4. LeetCode 111.二叉树的最小深度
题目链接:https://leetcode.cn/problems/minimum-depth-of-binary-tree/description/
文章链接:https://programmercarl.com/0111.二叉树的最小深度.html#思路
视频链接:https://www.bilibili.com/video/BV1QD4y1B7e2
思路:
本题依然是前序遍历和后序遍历都可以,前序求的是深度,后序求的是高度。
使用后序遍历,其实求的是根节点到叶子节点的最小距离,就是求高度的过程,不过这个最小距离 也同样是最小深度。
注意:求最小深度时并不是简单的求左右子树深度的最小值,而是需要判断:
如果左子树为空,右子树不为空,说明最小深度是 1 + 右子树的深度。
反之,右子树为空,左子树不为空,最小深度是 1 + 左子树的深度。
如果左右子树都不为空,返回左右子树深度最小值 + 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 {
// int deep = 0;
// public int minDepth(TreeNode root) {
// find(root,0);
// return deep;
// }
// // 前序遍历
// public void find(TreeNode cur,int curDeep){
// if (cur==null) {
// return;
// }
// curDeep++;
// if (cur.left == null && cur.right == null) {
// if (deep == 0 || curDeep < deep) {
// deep = curDeep; // 更新全局变量deep
// }
// }
// find(cur.left,curDeep);
// find(cur.right,curDeep);
// }
public int minDepth(TreeNode root) {
//return minDeep(root);// 后序遍历
return minDeep2(root);// 层序遍历
}
// 后序遍历
public int minDeep(TreeNode cur) {
if (cur == null) return 0;
int left = minDeep(cur.left);
int right = minDeep(cur.right);
if (cur.left == null && cur.right != null) {
return 1 + right;
} else if (cur.left != null && cur.right == null) {
return 1 + left;
}
return 1 + Math.min(left,right);
}
// 层序遍历
public int minDeep2(TreeNode root) {
if(root == null) return 0;
Deque<TreeNode> deque = new ArrayDeque<>();
deque.offer(root);
int depth = 0;// 深度
while(!deque.isEmpty()) { // 对当前层进行分析
int len = deque.size();// 当前层的节点数
depth++;
while(len>0) {
TreeNode cur = deque.poll();
if(cur.left == null && cur.right == null) {
return depth;
}
if (cur.left != null) deque.offer(cur.left);
if (cur.right != null) deque.offer(cur.right);
len--;
}
}
return depth;
}
}