110.平衡二叉树
题目链接:https://leetcode.com/problems/balanced-binary-tree
给定一个二叉树,判断它是否是高度平衡的二叉树。
本题中,一棵高度平衡二叉树定义为:一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。
示例 1:
给定二叉树 [1,2,2,3,3,null,null,4,4]
返回 false 。
思路:
- 复习:深度(根节点到该节点)高度(该节点到叶子节点)求二叉树最大深度 = 求根节点高度 = 后序遍历 直接求深度(需要递归回溯)
- 本题:递归,注意递归函数返回值以及平衡的定义
class Solution {
public boolean isBalanced(TreeNode root) {
int height = getHeight(root);
if (height == -1) {
return false;
}
return true;
}
// step1: 明确递归函数的参数和返回值
// -1 表示已经不是平衡二叉树了,否则返回值是以该节点为根节点树的高度
private int getHeight(TreeNode node) {
if (node == null) {
return 0;
}
int leftHeight = getHeight(node.left); //左
int rightHeight = getHeight(node.right); //右
if (leftHeight == -1 || rightHeight == -1) {
return -1;
}
if (Math.abs(leftHeight - rightHeight) > 1){ //中,差值大于1,不平衡
return -1;
}
return 1 + Math.max(leftHeight, rightHeight);
}
}
257. 二叉树的所有路径
题目链接:https://leetcode.com/problems/binary-tree-paths
思路:
- 回溯要和递归永远在一起,世界上最遥远的距离是你在花括号里,而我在花括号外!
- 回溯可能隐藏在函数调用时的参数赋值里!
方法一:递归法(前序遍历,递归与回溯永远一起)
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<Integer> path = new ArrayList<>();
List<String> result = new ArrayList<>();
if (root == null) return result;
traversal(root, path, result);
return result;
}
private void traversal(TreeNode node, List<Integer> path, List<String> result) {
path.add(node.val); //前序遍历,中,必须在前面因为需要添加最后的叶子节点
// step2: 终止条件
if (node.left == null && node.right == null) {
// add this path to result
StringBuilder sb = new StringBuilder(); // StringBuilder用来拼接字符串
for (int i = 0; i < path.size()-1; i++) {
sb.append(path.get(i)).append("->");
}
sb.append(path.get(path.size()-1)); //最后一个
result.add(sb.toString());
return;
}
// 递归和回溯是同时进行,所以要放在同一个花括号里
if (node.left != null) { // 左
traversal(node.left, path, result);
path.remove(path.size()-1); // 回溯
}
if (node.right != null) { //右
traversal(node.right, path, result);
path.remove(path.size()-1); //回溯
}
return;
}
}
方法二:递归法(简化)
函数参数,string拷贝赋值,每次调用函数,path依然没有加上->,没有被改变,这其实隐藏了回溯
如果把 path + "->"作为函数参数就是可以的,因为并没有改变path的数值,执行完递归函数之后,path依然是之前的数值(相当于回溯了)
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> result = new ArrayList<>();
if (root == null) return result; // 注意边界
traversal(root, "", result);
return result;
}
private void traversal(TreeNode node, String path, List<String> result) {
path += node.val;
if (node.left == null && node.right == null){ // 终止条件
result.add(path);
}
if (node.left != null) {
traversal(node.left, path + "->", result); // 有省略的回溯
}
if (node.right != null) {
traversal(node.right, path + "->", result);
}
}
}
404.左叶子之和
题目链接:https://leetcode.com/problems/sum-of-left-leaves
思路:
- 节点的左孩子不为空,且左孩子的左右孩子都为空(说明是叶子节点)
- 判断当前节点是不是左叶子是无法判断的,必须要通过节点的父节点来判断其左孩子是不是左叶子。
本题重点在于怎么通过父节点的属性来判定左节点的属性。 - 后序遍历,通过递归函数的返回值来累加求取左叶子数值之和
方法一:递归法
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if (root == null) return 0;
if (root.left == null && root.right == null) { // leaf node
return 0;
}
TreeNode leftNode = root.left; // 左
TreeNode rightNode = root.right; // 右
int leftSum = 0;
if (leftNode!= null) { // left node
if (leftNode.left == null && leftNode.right == null) {// left leaf node
leftSum = leftNode.val;
} else {
leftSum = sumOfLeftLeaves(leftNode);
}
}
int rightSum = sumOfLeftLeaves(rightNode);
return leftSum + rightSum; // 中
}
}
方法二:迭代法,前中后序都可以
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if (root == null) {
return 0;
}
Deque<TreeNode> stack = new LinkedList<>();
stack.push(root);
int sum = 0;
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
if (node.left != null) {
if (node.left.left == null && node.left.right == null) {
sum += node.left.val;
}
}
if (node.left != null) stack.push(node.left);
if (node.right != null) stack.push(node.right);
}
return sum;
}
}