前言
本文属于特定的六道题目题解和调试代码。
1 ✔ [129]求根到叶子节点数字之和 Medium 2024-04-02 82
2 ✔ [104]二叉树的最大深度 Easy 2024-05-06 81
3 ✔ [101]对称二叉树 Easy 2024-03-08 80
4 ✔ [144]二叉树的前序遍历 Easy 2024-05-09 79
5 ✔ [110]平衡二叉树 Easy 2024-04-08 79
6 ✔ [543]二叉树的直径 Easy 2024-02-05 75
正所谓磨刀不误砍柴功,下面这几篇文章算是我刷题的经验帖,对于涉及的题型,数据结构的分析解决很有帮助,这里放个链接仅供参考。
按照树形结构直观地打印出一棵二叉树、快速创建leetcode中树的结构(Java)
leetCode热题58-63 解题代码,调试代码和思路
1 ✔ [129]求根到叶子节点数字之和 Medium 2024-04-02 82
>//给你一个二叉树的根节点 root ,树中每个节点都存放有一个 0 到 9 之间的数字。
//
//
//
// 每条从根节点到叶节点的路径都代表一个数字:
//
//
//
//
// 例如,从根节点到叶节点的路径 1 -> 2 -> 3 表示数字 123 。
//
//
// 计算从根节点到叶节点生成的 所有数字之和 。
// 叶节点 是指没有子节点的节点。
//
//
// 示例 1:
//
//
//输入:root = [1,2,3]
//输出:25
//解释:
//从根到叶子节点路径 1->2 代表数字 12
//从根到叶子节点路径 1->3 代表数字 13
//因此,数字总和 = 12 + 13 = 25
//
// 示例 2:
//
//
//输入:root = [4,9,0,5,1]
//输出:1026
//解释:
//从根到叶子节点路径 4->9->5 代表数字 495
//从根到叶子节点路径 4->9->1 代表数字 491
//从根到叶子节点路径 4->0 代表数字 40
//因此,数字总和 = 495 + 491 + 40 = 1026
//
//
//
//
// 提示:
//
//
// 树中节点的数目在范围 [1, 1000] 内
// 0 <= Node.val <= 9
// 树的深度不超过 10
//
//
// Related Topics 树 深度优先搜索 二叉树 👍 738 👎 0
自测代码
import cn.wanghaixin.solution.utils.TreeNode;
/**
* 求根节点到叶节点数字之和
*
* @author Wang Hai Xin
* @date 2024
*/
public class P129_SumRootToLeafNumbers{
public static void main(String[] args) {
Solution solution = new P129_SumRootToLeafNumbers().new Solution();
TreeNode treeNode = new TreeNode("[0,1]");
int i = solution.sumNumbers(treeNode);
System.out.println(i);
// TO TEST
}
//leetcode submit region begin(Prohibit modification and deletion)
/**
* 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 sumNumbers(TreeNode root) {
int i = numString(root, new StringBuffer());
return i;
// int result = 0 ;
//
// TreeNode temp = root;
//
// StringBuffer stringBuffer = new StringBuffer();
//
// StringBuffer append = stringBuffer.append(temp.val);
//
// while (temp.left != null){
// temp = temp.left;
// append.append(temp.val);
// }
// 左遍历完
// 转化为数字
// 右遍历完
// 转化为数字
}
public int numString(TreeNode temp,StringBuffer result){
if (temp.left == null&&temp.right ==null) {
return Integer.parseInt(result.append(temp.val).toString());
}
result.append(temp.val);
String tempStr = result.toString();
int leftNum = 0;
if (temp.left != null) {
leftNum = numString(temp.left, result);
result = new StringBuffer(tempStr);
}
int rightNum = 0;
if (temp.right != null) {
rightNum = numString(temp.right, result);
}
return leftNum+rightNum;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}
思路:分析清楚递归的四要素,
- 基本情况:递归的终止条件,也就是出口
- 递归调用:在递归函数内部调用自身,从而将原始问题分解为更小的问题
- 问题规模减小: 每次调用都应该使得问题规模减小,向着基本情况靠拢
- 组合子问题的解:将子问题的解组合起来,得到原始问题的解。
最基本事件,最小的一个事件,重复单位。然后想如何联系起来,想清楚出口和入口。
提交代码
class Solution {
public int sumNumbers(TreeNode root) {
int i = numString(root, new StringBuffer());
return i;
// int result = 0 ;
//
// TreeNode temp = root;
//
// StringBuffer stringBuffer = new StringBuffer();
//
// StringBuffer append = stringBuffer.append(temp.val);
//
// while (temp.left != null){
// temp = temp.left;
// append.append(temp.val);
// }
// 左遍历完
// 转化为数字
// 右遍历完
// 转化为数字
}
public int numString(TreeNode temp,StringBuffer result){
if (temp.left == null&&temp.right ==null) {
return Integer.parseInt(result.append(temp.val).toString());
}
result.append(temp.val);
String tempStr = result.toString();
int leftNum = 0;
if (temp.left != null) {
leftNum = numString(temp.left, result);
result = new StringBuffer(tempStr);
}
int rightNum = 0;
if (temp.right != null) {
rightNum = numString(temp.right, result);
}
return leftNum+rightNum;
}
}
2 ✔ [104]二叉树的最大深度 Easy 2024-05-06 81
>//给定一个二叉树 root ,返回其最大深度。
//
// 二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。
//
//
//
// 示例 1:
//
//
//
//
//
//
//输入:root = [3,9,20,null,null,15,7]
//输出:3
//
//
// 示例 2:
//
//
//输入:root = [1,null,2]
//输出:2
//
//
//
//
// 提示:
//
//
// 树中节点的数量在 [0, 10⁴] 区间内。
// -100 <= Node.val <= 100
//
//
// Related Topics 树 深度优先搜索 广度优先搜索 二叉树 👍 1824 👎 0
自测代码
import cn.wanghaixin.solution.utils.TreeNode;
import javax.validation.constraints.Max;
/**
* 二叉树的最大深度
*
* @author Wang Hai Xin
* @date 2024
*/
public class P104_MaximumDepthOfBinaryTree{
public static void main(String[] args) {
Solution solution = new P104_MaximumDepthOfBinaryTree().new Solution();
int i = solution.maxDepth(new TreeNode("[3,9,20,null,null,15,7]"));
System.out.println(i);
// TO TEST
}
//leetcode submit region begin(Prohibit modification and deletion)
/**
* 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 maxDepth(TreeNode root) {
if (root== null ) {
return 0;
}
return solution(root);
}
public int solution(TreeNode root){
if (root.right == null && root.left == null) {
return 1;
}
if (root.right ==null) {
return 1 + solution(root.left);
}else if (root.left == null){
return 1 +solution(root.right);
}else {
return 1+Math.max(solution(root.left),solution(root.right));
}
}
}
//leetcode submit region end(Prohibit modification and deletion)
}
思路:简单递归,和上面一样
提交代码
class Solution {
public int maxDepth(TreeNode root) {
if (root== null ) {
return 0;
}
return solution(root);
}
public int solution(TreeNode root){
if (root.right == null && root.left == null) {
return 1;
}
if (root.right ==null) {
return 1 + solution(root.left);
}else if (root.left == null){
return 1 +solution(root.right);
}else {
return 1+Math.max(solution(root.left),solution(root.right));
}
}
}
3 ✔ [101]对称二叉树 Easy 2024-03-08 80
>//给你一个二叉树的根节点 root , 检查它是否轴对称。
//
//
//
// 示例 1:
//
//
//输入:root = [1,2,2,3,4,4,3]
//输出:true
//
//
// 示例 2:
//
//
//输入:root = [1,2,2,null,3,null,3]
//输出:false
//
//
//
//
// 提示:
//
//
// 树中节点数目在范围 [1, 1000] 内
// -100 <= Node.val <= 100
//
//
//
//
// 进阶:你可以运用递归和迭代两种方法解决这个问题吗?
//
// Related Topics 树 深度优先搜索 广度优先搜索 二叉树 👍 2708 👎 0
自测代码
import cn.wanghaixin.solution.utils.TreeNode;
/**
* 对称二叉树
*
* @author Wang Hai Xin
* @date 2024
*/
public class P101_SymmetricTree{
public static void main(String[] args) {
Solution solution = new P101_SymmetricTree().new Solution();
// TO TEST
}
//leetcode submit region begin(Prohibit modification and deletion)
/**
* 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) {
return check(root,root);
}
public Boolean check(TreeNode left,TreeNode right){
if (left == null && right == null){
return true;
}
if (left == null || right == null){
return false;
}
return left.val == right.val && check(left.left,right.right) && check(left.right,right.left);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}
思路:主要是找到了什么情况下算是对称
- 左边的一定等于右边的
提交代码
/**
* 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) {
return check(root,root);
}
public Boolean check(TreeNode left,TreeNode right){
if (left == null && right == null){
return true;
}
if (left == null || right == null){
return false;
}
return left.val == right.val && check(left.left,right.right) && check(left.right,right.left);
}
}
4 ✔ [144]二叉树的前序遍历 Easy 2024-05-09 79
>//给你二叉树的根节点 root ,返回它节点值的 前序 遍历。
//
//
//
// 示例 1:
//
//
//输入:root = [1,null,2,3]
//输出:[1,2,3]
//
//
// 示例 2:
//
//
//输入:root = []
//输出:[]
//
//
// 示例 3:
//
//
//输入:root = [1]
//输出:[1]
//
//
// 示例 4:
//
//
//输入:root = [1,2]
//输出:[1,2]
//
//
// 示例 5:
//
//
//输入:root = [1,null,2]
//输出:[1,2]
//
//
//
//
// 提示:
//
//
// 树中节点数目在范围 [0, 100] 内
// -100 <= Node.val <= 100
//
//
//
//
// 进阶:递归算法很简单,你可以通过迭代算法完成吗?
//
// Related Topics 栈 树 深度优先搜索 二叉树 👍 1249 👎 0
自测代码
import cn.wanghaixin.solution.utils.TreeNode;
import java.util.ArrayList;
import java.util.List;
/**
* 二叉树的前序遍历
*
* @author Wang Hai Xin
* @date 2024
*/
public class P144_BinaryTreePreorderTraversal{
public static void main(String[] args) {
Solution solution = new P144_BinaryTreePreorderTraversal().new Solution();
// TO TEST
}
//leetcode submit region begin(Prohibit modification and deletion)
/**
* 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<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> result = new ArrayList<>();
preList(root,result);
return result;
}
public void preList(TreeNode root,ArrayList<Integer> result){
if (root == null) {
return ;
}
result.add(root.val);
preList(root.left,result);
preList(root.right,result);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}
提交代码
/**
* 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<Integer> preorderTraversal(TreeNode root) {
ArrayList<Integer> result = new ArrayList<>();
preList(root,result);
return result;
}
public void preList(TreeNode root,ArrayList<Integer> result){
if (root == null) {
return ;
}
result.add(root.val);
preList(root.left,result);
preList(root.right,result);
}
}
5 ✔ [110]平衡二叉树 Easy 2024-04-08 79
>//给定一个二叉树,判断它是否是 平衡二叉树
//
//
//
// 示例 1:
//
//
//输入:root = [3,9,20,null,null,15,7]
//输出:true
//
//
// 示例 2:
//
//
//输入:root = [1,2,2,3,3,null,null,4,4]
//输出:false
//
//
// 示例 3:
//
//
//输入:root = []
//输出:true
//
//
//
//
// 提示:
//
//
// 树中的节点数在范围 [0, 5000] 内
// -10⁴ <= Node.val <= 10⁴
//
//
// Related Topics 树 深度优先搜索 二叉树 👍 1505 👎 0
自测代码
import cn.wanghaixin.solution.utils.TreeNode;
import net.bytebuddy.implementation.bytecode.Throw;
import static cn.wanghaixin.solution.utils.TreeNode.TreeNodeShow;
/**
* 平衡二叉树
* 疑惑点: 错误理解了平衡树,以为要所有的叶子节点层数的差值都不能大于一,平衡二叉树是指所有的子二叉树都不大于1即可
* 下面的代码逻辑利用了,计算每一个节点的左边从叶子节点到本节点有几层,然后再计算该节点左边到叶子节点有几层,然后求差值
* @author Wang Hai Xin
* @date 2024
*/
public class P110_BalancedBinaryTree{
public static void main(String[] args) {
Solution solution = new P110_BalancedBinaryTree().new Solution();
TreeNode treeNode = new TreeNode("[1,2,3,4,5,6,null,8]");
System.out.println(treeNode.toString());
TreeNodeShow(treeNode);
System.out.println(solution.isBalanced(treeNode) );
// TO TEST
}
//leetcode submit region begin(Prohibit modification and deletion)
/**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 {
public boolean isBalanced(TreeNode root) {
try {
height(root);
} catch (Exception e) {
return false;
}
return true;
}
public int height(TreeNode root) throws Exception {
if (root == null) {
return 0;
}
int leftHeight = height(root.left);
int rightHeight = height(root.right);
if (leftHeight == -1 || rightHeight == -1 || Math.abs(leftHeight - rightHeight) > 1) {
throw new Exception();
} else {
return Math.max(leftHeight, rightHeight) + 1;
}
}
}
//leetcode submit region end(Prohibit modification and deletion)
}
思路:下面的代码逻辑利用了,计算每一个节点的左边从叶子节点到本节点有几层,然后再计算该节点左边到叶子节点有几层,然后求差值
提交代码
/**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 {
public boolean isBalanced(TreeNode root) {
try {
height(root);
} catch (Exception e) {
return false;
}
return true;
}
public int height(TreeNode root) throws Exception {
if (root == null) {
return 0;
}
int leftHeight = height(root.left);
int rightHeight = height(root.right);
if (leftHeight == -1 || rightHeight == -1 || Math.abs(leftHeight - rightHeight) > 1) {
throw new Exception();
} else {
return Math.max(leftHeight, rightHeight) + 1;
}
}
}
6 ✔ [543]二叉树的直径 Easy 2024-02-05 75
>//给你一棵二叉树的根节点,返回该树的 直径 。
//
// 二叉树的 直径 是指树中任意两个节点之间最长路径的 长度 。这条路径可能经过也可能不经过根节点 root 。
//
// 两节点之间路径的 长度 由它们之间边数表示。
//
//
//
// 示例 1:
//
//
//输入:root = [1,2,3,4,5]
//输出:3
//解释:3 ,取路径 [4,2,1,3] 或 [5,2,1,3] 的长度。
//
//
// 示例 2:
//
//
//输入:root = [1,2]
//输出:1
//
//
//
//
// 提示:
//
//
// 树中节点数目在范围 [1, 10⁴] 内
// -100 <= Node.val <= 100
//
//
// Related Topics 树 深度优先搜索 二叉树 👍 1535 👎 0
思路 仔细分析问题,问题是找任意两个点的长度,任意两个点,任意两个点就不好确定,可以转化为一个点作为根左边最长加上右边最长就是经过这个节点向下的最长路径,向上就只需要再加1. 然后依次类推计算出所有但做根时的最长进行比较
自测代码
import cn.wanghaixin.solution.utils.TreeNode;
import static cn.wanghaixin.solution.utils.TreeNode.TreeNodeShow;
/**
* 二叉树的直径
*
* @author Wang Hai Xin
* @date 2024
*/
public class P543_DiameterOfBinaryTree{
public static void main(String[] args) {
Solution solution = new P543_DiameterOfBinaryTree().new Solution();
TreeNode treeNode = new TreeNode("[1,2,3,4,5]");
TreeNodeShow(treeNode);
System.out.println(solution.diameterOfBinaryTree(treeNode));
// TO TEST
}
//leetcode submit region begin(Prohibit modification and deletion)
/**
* 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 res = 0;
public int diameterOfBinaryTree(TreeNode root) {
detph(root);
return res-1;
}
public int detph(TreeNode root){
if (root == null) {
return 0;
}
int l = detph(root.left);
int r = detph(root.right);
res = Math.max(res,l+r+1);
return Math.max(l,r)+1;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}
提交代码
/**
* 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 Solutio
1276

被折叠的 条评论
为什么被折叠?



