当前位置: 首页 > news >正文

力扣labuladong——一刷day55

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 一、力扣951. 翻转等价二叉树
  • 二、力扣124. 二叉树中的最大路径和
  • 三、力扣112. 路径总和(遍历)
  • 四、力扣112. 路径总和(分解)


前言


二叉树的遍历代码是动态规划和回溯算法的祖宗。 动态规划 的关键在于明确递归函数的定义,把用子问题的结果推导出大问题的结果。 回溯算法 就简单粗暴多了,就是单纯的遍历回溯树。

一、力扣951. 翻转等价二叉树

/*** 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 flipEquiv(TreeNode root1, TreeNode root2) {if(root1 == null && root2 == null){return true;}if(root1 == null || root2 == null){return false;}if(root1.val != root2.val){return false;}return (flipEquiv(root1.left,root2.left) && flipEquiv(root1.right,root2.right)) || (flipEquiv(root1.left,root2.right) && flipEquiv(root1.right,root2.left));}
}

二、力扣124. 二叉树中的最大路径和

/*** 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 = Integer.MIN_VALUE;public int maxPathSum(TreeNode root) {fun(root);return res;}public int fun(TreeNode root){if(root == null){return 0;}int l = Math.max(0,fun(root.left));int r = Math.max(0,fun(root.right));res = Math.max(res,l+r+root.val);return Math.max(l,r) + root.val;}
}

三、力扣112. 路径总和(遍历)

/*** 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 {boolean flag = false;public boolean hasPathSum(TreeNode root, int targetSum) {if(root == null){return false;}fun(root,targetSum,0);return flag;}public void fun(TreeNode root, int targetSum, int path){if(root == null){return;}if(root.left == null && root.right == null){if(path + root.val == targetSum){flag = true;}return;}fun(root.left,targetSum,path+root.val);fun(root.right,targetSum,path+root.val);}
}

四、力扣112. 路径总和(分解)

/*** 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 hasPathSum(TreeNode root, int targetSum) {if(root == null){return false;}if(root.left == root.right && root.val == targetSum){return true;}return hasPathSum(root.left,targetSum - root.val) || hasPathSum(root.right,targetSum-root.val);}
}
http://www.lryc.cn/news/246314.html

相关文章:

  • springboot实现验证码功能
  • 内测分发平台是否支持应用的微服务化部署
  • 1140. 最短网络,prim算法,模板题
  • 升级jdk17过程中,原来的jdk8下的webservice客户端怎样处理
  • Verilog基本语法概述
  • 论文阅读:C2VIR-SLAM: Centralized Collaborative Visual-Inertial-Range SLAM
  • 蓝桥杯刷题day01——字符串中的单词反转
  • Python---引用变量与可变、非可变类型
  • GDOUCTF2023-Reverse WP
  • Day43力扣打卡
  • elementui的table合并列,三个一组
  • HarmonyOS-Service服务开发(一)
  • FLASK博客系列4——再谈路由
  • sql之left join、right join、inner join的区别
  • 京东秒杀之秒杀详情
  • mobaxterm 下载、安装、使用
  • 办公技巧:Word中插入图片、形状、文本框排版技巧
  • apple macbook M系列芯片安装 openJDK17
  • C语言——打印出所有的“水仙花数”
  • <HarmonyOS第一课>应用程序框架 【课后考核】
  • 自动驾驶学习笔记(十一)——高精地图
  • HCIA-H12-811题目解析(2)
  • Docker-简介、基本操作
  • Codeforces Round 911 (Div. 2)(C dp D gcd 分解+容斥 E tarjan+dp)
  • 给csgo游戏搬砖新手的十大建议
  • 西南科技大学模拟电子技术实验一(常用电子仪器的使用及电子元器件的识别)预习报告
  • 回归分析例题(多元统计分析期末复习)
  • Linux多路转接select,poll
  • 如何轻松将 4K 转换为 1080p 高清视频
  • 责任链模式 (Chain of Responsibility Pattern)