今日内容
- leetcode. 110 平衡二叉树
- leetcode. 257 二叉树的所有路径
- leetcode. 404 左叶子之和
- leetcode. 222 完全二叉树的节点个数
Leetcode. 110 平衡二叉树
平衡二叉树的定义是:左右子树的高度差绝对值不大于 1.
在二叉树中,高度和深度是两个看似相同实则有很大区别的概念:
- 高度:当前节点到叶节点的距离
- 深度:根节点到当前节点的距离
既然要求的是高度,那么就是从叶节点开始计算,从底向上遍历。这种情况就非常适合后序遍历,先算出左右子树的高度,计算它们间的高度差,最后进行判断。
代码如下:
class Solution {
public boolean isBalanced(TreeNode root) {
// 高度:当前节点到叶节点的距离
// 所以需要从下到上遍历,这样适合后序遍历
return nodeHeight(root) == -1 ? false : true;
}
public int nodeHeight(TreeNode root){
if (root == null){return 0;}
int leftHeight = nodeHeight(root.left);
if (leftHeight == -1){return -1;}
int rightHeight = nodeHeight(root.right);
if (rightHeight == -1){return -1;}
return Math.abs(leftHeight - rightHeight) > 1 ? -1 : Math.max(leftHeight, rightHeight) + 1;
}
}
- 时间复杂度:O(n)
- 空间复杂度:O(n)
Leetcode. 257 二叉树的所有路径
本题要求从根节点开始到叶子节点的所有路径,所以使用前序遍历是最适合的。并且本题也是第一次接触到回溯的思想,具体的回溯概念在后续系列中会提到,此处就先有个印象,记住 递归和回溯是相辅相成的。
在本题中,回溯体现在路径的回退上。在记录了一条路径后,需要回退之后再记录另一条路径。
本题的基本思路可以用下图概括:
根据上述思路,写出如下代码:
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> result = new ArrayList<>(); // 记录最终结果
List<Integer> path = new ArrayList<>(); // 记录每一条路径上节点的值
pathTraverse(root, result, path);
return result;
}
public void pathTraverse(TreeNode node, List<String> result, List<Integer> path){
path.add(node.val);
// 碰到叶子节点后就结束递归
// 将 int 的结果转换为 String
if (node.left == null && node.right == null){
String sResult;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < path.size() - 1; i++){
sb.append(path.get(i));
sb.append("->");
}
sb.append(path.get(path.size() - 1));
sResult = sb.toString();
result.add(sResult);
}
// 若左子树存在,则进入递归
if (node.left != null){
pathTraverse(node.left, result, path);
path.remove(path.size() - 1); // 回溯
}
// 若右子树存在,则进入递归
if (node.right != null){
pathTraverse(node.right, result, path);
path.remove(path.size() - 1); // 回溯
}
}
}
- 时间复杂度:O(n)
- 空间复杂度:O(n)
Leetcode. 404 左叶子之和
本题与之前的题目有所不同,之前的题目基本上都是判断节点是否为叶子节点,但是不知道它是左叶子还是右叶子。而判断是否为左右叶子就需要知晓其根节点。
这就是本题与之前题目的区别。
我们可以写出这样的代码:
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if (root == null){return 0;}
if (root.left == null && root.right == null){return 0;} // 如果左右子树为空,则左叶子和为0. 这行可省略,省略后就是会多递归一层
int leftSum = sumOfLeftLeaves(root.left);
if (root.left != null && root.left.left == null && root.left.right == null){ // 若左子树就是左叶子,就直接取值
leftSum = root.left.val;
}
int rightSum = sumOfLeftLeaves(root.right); // 跳到右子树进行操作
int sum = leftSum + rightSum;
return sum;
}
}
- 时间复杂度:O(n)
- 空间复杂度:O(log n)
Leetcode. 222 完全二叉树的节点个数
本题完全可以使用前序或后序遍历来完成。
代码如下:
// 前序遍历
class Solution {
int sum = 0;
public int countNodes(TreeNode root) {
if (root == null){return 0;}
sum += 1;
int leftSum = countNodes(root.left);
int rightSum = countNodes(root.right);
return sum;
}
}
// 后序遍历
class Solution {
public int countNodes(TreeNode root) {
if (root == null){return 0;}
int leftSum = 0;
int rightSum = 0;
if (root.left != null){
leftSum = countNodes(root.left);
}
if (root.right != null){
rightSum = countNodes(root.right);
}
int sum = leftSum + rightSum + 1;
return sum;
}
}
- 时间复杂度:O(n)
- 空间复杂度:O(log n)
总结
通过这次练习,对二叉树的递归遍历有了更清晰的认知,起码能对前中后序的使用场景搭建出一个基本的框架。最近开始忙起来了,题目也都是没接触过的,但还是要坚持下去!