110.平衡二叉树
- 刷题https://leetcode.cn/problems/balanced-binary-tree/description/
- 文章讲解https://programmercarl.com/0110.%E5%B9%B3%E8%A1%A1%E4%BA%8C%E5%8F%89%E6%A0%91.html
- 视频讲解https://www.bilibili.com/video/BV1Ug411S7my/?vd_source=af4853e80f89e28094a5fe1e220d9062
-
题解(递归):
/*** 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 int getHeight(TreeNode root){//若遍历到空结点,说明为最小高度0if(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;}public boolean isBalanced(TreeNode root) {return getHeight(root) != -1;}
}
257. 二叉树的所有路径
- 刷题https://leetcode.cn/problems/binary-tree-paths/description/
- 文章讲解https://programmercarl.com/0257.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E6%89%80%E6%9C%89%E8%B7%AF%E5%BE%84.html
- 视频讲解https://www.bilibili.com/video/BV1ZG411G7Dh/?vd_source=af4853e80f89e28094a5fe1e220d9062
-
题解(递归):
/*** 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 List<String> binaryTreePaths(TreeNode root) {List<String> result = new ArrayList<>();if(root == null){return result;}List<Integer> paths = new ArrayList<>();traversal(root, paths, result);return result;}public void traversal(TreeNode root, List<Integer> paths, List<String> result){//中序中的中paths.add(root.val);//中序的左if(root.left != null){//递归,加入左结点元素traversal(root.left, paths, result);//回溯,路径中弹出最后一个元素paths.remove(paths.size() - 1);}//中序的右if(root.right != null){traversal(root.right, paths, result);paths.remove(paths.size() - 1);}//如果走到叶子结点,说明这条路走到底,将path中的路径进行处理并返回上一级if(root.left == null && root.right == null){StringBuilder sb = new StringBuilder();//字符串处理,将当前路径字符串做加箭头处理for(int i = 0; i < paths.size() - 1; i++){sb.append(paths.get(i)).append("->");}sb.append(paths.get(paths.size() - 1));//箭头处理结束,转为字符串result.add(sb.toString());//该条路径处理结束,返回继续下一条路径的回溯return;}}
}
404.左叶子之和
- 刷题https://leetcode.cn/problems/sum-of-left-leaves/description/
- 文章讲解https://programmercarl.com/0404.%E5%B7%A6%E5%8F%B6%E5%AD%90%E4%B9%8B%E5%92%8C.html
- 视频讲解https://www.bilibili.com/video/BV1GY4y1K7z8/?vd_source=af4853e80f89e28094a5fe1e220d9062
-
题解(递归):
/*** 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 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;}
}