代码随想录算法训练营第十七天 | 110.平衡二叉树、 **257. 二叉树的所有路径、 **404.左叶子之和
今天的题都采用递归完成,因为递归相对比较好理解,且时间、空间复杂度低
110.平衡二叉树
题目链接:110. 平衡二叉树 - 力扣(LeetCode)
注意分清楚二叉树某个节点的 深度 和 高度 的区别
这道题求的是 高度
// 自己的递归法(递归中套了另外一个递归,可能更耗时?)
class Solution {
public boolean isBalanced(TreeNode root) {
if(root == null) return true;
// 递归查找左右子树的高度
int left = findDepth(root.left, 1);
int right = findDepth(root.right, 1);
int abs = Math.abs(left - right);
// 递归左右子树
return abs <= 1 && isBalanced(root.left) && isBalanced(root.right);
}
public int findDepth(TreeNode node, int count) {
if(node == null) return count;
count++;
int leftDepth = findDepth(node.left, count);
int rightDepth = findDepth(node.right, count);
return Math.max(leftDepth, rightDepth);
}
}
// Carl的递归法(更简洁,值得学习)
/**
1. 明确递归函数的参数和返回值
参数:当前传入节点。 返回值:以当前传入节点为根节点的树的高度。
那么如何标记左右子树是否差值大于1呢?
如果当前传入节点为根节点的二叉树已经不是二叉平衡树了,还返回高度的话就没有意义了。
所以如果已经不是二叉平衡树了,可以返回-1 来标记已经不符合平衡树的规则了。
2. 明确终止条件
递归的过程中依然是遇到空节点了为终止,返回0,表示当前节点为根节点的树高度为0
3. 明确单层递归的逻辑
分别求出其左右子树的高度,然后如果差值小于等于1,则返回当前二叉树的高度,否则返回-1,表示已经不是二叉平衡树了。
*/
class Solution {
/**
* 递归法
*/
public boolean isBalanced(TreeNode root) {
// 通过判断是否 == -1
return getHeight(root) != -1;
}
private int getHeight(TreeNode root) {
if (root == null) {
return 0;
}
int leftHeight = getHeight(root.left);
if (leftHeight == -1) {
return -1;
}
int rightHeight = getHeight(root.right);
if (rightHeight == -1) {
return -1;
}
// 左右子树高度差大于1,return -1表示已经不是平衡树了
if (Math.abs(leftHeight - rightHeight) > 1) {
return -1;
}
return Math.max(leftHeight, rightHeight) + 1;
}
}
257. 二叉树的所有路径
题目链接:257. 二叉树的所有路径 - 力扣(LeetCode)
就是回溯,套模板,处理单层逻辑细节
// 方法一,自己的,感觉对于自己来说更好理解一点
class Solution {
List<String> res;
StringBuilder s;
public List<String> binaryTreePaths(TreeNode root) {
res = new ArrayList<>();
s = new StringBuilder();
if(root == null) return res;
dfs(root);
return res;
}
public void dfs(TreeNode node) {
// 判断是否为叶子节点
if(node.left == null && node.right == null) {
String strNum = Integer.toString(node.val);
s.append(strNum);
// 这里就不添加"->"符号了
res.add(s.toString());
for(int i = 0; i < strNum.length(); ++i) {
s.deleteCharAt(s.length() - 1);
}
return;
}
String strNum = Integer.toString(node.val);
s.append(strNum);
s.append("->");
if(node.left != null)
dfs(node.left);
if(node.right != null)
dfs(node.right);
// 进行回溯,删除刚刚添加的数字和"->"符号
for(int i = 0; i < 2 + strNum.length(); ++i) {
s.deleteCharAt(s.length() - 1);
}
}
}
// 方式二,Carl的
class Solution {
/**
* 递归法
*/
public List<String> binaryTreePaths(TreeNode root) {
List<String> res = new ArrayList<>();// 存最终的结果
if (root == null) {
return res;
}
List<Integer> paths = new ArrayList<>();// 作为结果中的路径
traversal(root, paths, res);
return res;
}
private void traversal(TreeNode root, List<Integer> paths, List<String> res) {
paths.add(root.val);// 前序遍历,中
// 遇到叶子结点
if (root.left == null && root.right == null) {
// 输出
StringBuilder sb = new StringBuilder();// StringBuilder用来拼接字符串,速度更快
for (int i = 0; i < paths.size() - 1; i++) {
sb.append(paths.get(i)).append("->");
}
sb.append(paths.get(paths.size() - 1));// 记录最后一个节点
res.add(sb.toString());// 收集一个路径
return;
}
// 递归和回溯是同时进行,所以要放在同一个花括号里
if (root.left != null) { // 左
traversal(root.left, paths, res);
paths.remove(paths.size() - 1);// 回溯
}
if (root.right != null) { // 右
traversal(root.right, paths, res);
paths.remove(paths.size() - 1);// 回溯
}
}
}
404.左叶子之和
题目链接:404. 左叶子之和 - 力扣(LeetCode)
这道题不太会,明天看视频学习一下
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if (root == null) return 0;
int leftValue = sumOfLeftLeaves(root.left); // 左
int rightValue = sumOfLeftLeaves(root.right); // 右
int midValue = 0;
if (root.left != null && root.left.left == null && root.left.right == null) {
midValue = root.left.val;
}
int sum = midValue + leftValue + rightValue; // 中
return sum;
}
}
总结:
前两题都能胡乱ac,第三题不太会。感觉二叉树递归始终没有章法,明天或后天抽时间看Carl视频再总结一下