当前位置:网站首页>递归处理——子问题
递归处理——子问题
2022-07-26 05:54:00 【学习追求高效率】
首先:什么是子问题
也就是把整体问题,可以浓缩
比如找树的左边右边
就可以当做找 一个节点的 左节点 右节点
5. 检测值为value的元素是否存在
子问题递归思想:原则就是只根据一个根节点分析就好
原则就是只根据一个根节点分析就好
分析如下:
分析A节点
如果A节点的值是 key
那么返回A节点
否则遍历左节点
如果左节点返回的值不是 空 那么就返回返回值
如果左节点返回值是空 那么就 遍历右节点
如果右节点的返回值不是空 那么就返回返回值
否则就直接返回空
// 检测值为value的元素是否存在
TreeNode find(TreeNode root, char val) {
if(root == null) return null;
if(root.val == val) {
return root;
}
TreeNode ret1 = find(root.left,val);
if(ret1 != null) {
return ret1;
}
TreeNode ret2 = find(root.right,val);
if(ret2 != null) {
return ret2;
}
return null;
}
边栏推荐
- 520 for what? DIY is a high-value RGB clock that girls want to watch
- leetcode-Array
- 5-year-old Test Engineer - how to choose the next step?
- [personal summary] end of July 24, 2022
- ament_cmake生成ROS2库并链接
- Hack the box - Introduction to networking module detailed Chinese tutorial
- 日志收集分析平台搭建-1-环境准备
- 开发项目事半功倍,一款开源的stm32驱动库大集合
- How can programmers improve mental internal friction?
- Efficient, reliable and safe open source solution for serial communication
猜你喜欢

Talking about the practice of software defect management

对接微信支付(二)统一下单API

Why can't lpddr completely replace DDR?

leetcode-aboutString

Embedded general learning route arrangement

Mysql45 talking about global lock, table lock and row lock

语法泛化三种可行方案介绍

Kingbasees SQL language reference manual of Jincang database (10. Query and sub query)

金仓数据库 KingbaseES SQL 语言参考手册 (6. 表达式)

卸载手机自带APP的操作步骤
随机推荐
日志收集分析平台搭建-1-环境准备
Ros2 preliminary: basic communication with topic
2022 National latest fire-fighting facility operator (Senior fire-fighting facility operator) simulation test questions and answers
L. Link with Level Editor I dp
FTP experiment and overview
金仓数据库 KingbaseES SQL 语言参考手册 (6. 表达式)
Realize channel routing based on policy mode
Chapter 1 - Construction of development environment
[cloud native] record of feign custom configuration of microservices
Two auxiliary functions of integral Mall for business user operation
为什么LPDDR不能完全代替DDR?
金仓数据库 KingbaseES SQL 语言参考手册 (8. 函数(十))
【2023杰理科技提前批笔试题】~ 题目及参考答案
Learn about spark project on nebulagraph
Kingbasees SQL language reference manual of Jincang database (6. Expression)
Use latex to typeset multiple-choice test paper
Dynamic memory management and flexible array
对接微信支付(二)统一下单API
Another open source artifact, worth collecting and learning!
leetcode-aboutString
