当前位置:网站首页>leetcode刷题:二叉树01(二叉树的前序遍历)
leetcode刷题:二叉树01(二叉树的前序遍历)
2022-07-01 19:16:00 【涛涛英语学不进去】
144.二叉树的前序遍历
1.确定递归函数的参数和返回值
2.确定终止条件
3.确定单层递归的逻辑
package com.programmercarl.tree;
import java.util.ArrayList;
import java.util.List;
/** * @ClassName PreorderTraversal * @Descriotion TODO * @Author nitaotao * @Date 2022/7/1 15:21 * @Version 1.0 * 144. 二叉树的前序遍历 * https://leetcode.cn/problems/binary-tree-preorder-traversal/ **/
public class PreorderTraversal {
/** * 前序遍历,根左右。 * @param root * @return */
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
traversal(root, result);
return result;
}
/** * 前序遍历,根左右。 * * @param root * @param result * 1.确定递归函数的参数和返回值 * 2.确定终止条件 * 3.确定单层递归的逻辑 */
public void traversal(TreeNode root, List<Integer> result) {
//终止条件
if (root == null) {
return;
}
//访问根结点
result.add(root.val);
//访问左子树
traversal(root.left, result);
//访问右子树
traversal(root.right,result);
}
}
边栏推荐
- EDA工具对芯片产业的重要性知识科普
- Use Zadig to build a continuous delivery platform from 0 to 1
- Exclusive news: Alibaba cloud quietly launched RPA cloud computer and has opened cooperation with many RPA manufacturers
- 目標檢測——Yolo系列
- Vulnerability recurrence - Net ueeditor upload
- Win11如何取消任务栏隐藏?Win11取消任务栏隐藏的方法
- 喜马拉雅自研网关架构演进过程
- Test of NSI script
-
- 随机头像大全,多分类带历史记录微信小程序源码_支持流量主
猜你喜欢
RichView 文档中的 ITEM
Big factories are wolves, small factories are dogs?
[multithreading] realize the singleton mode (hungry and lazy) realize the thread safe singleton mode (double validation lock)
天气预报小程序源码 天气类微信小程序源码
EURA欧瑞E1000系列变频器使用PID实现恒压供水功能的相关参数设置及接线
王者战力查询改名工具箱小程序源码-带流量主激励广告
Redis installation and startup in Windows environment (background startup)
NSI脚本的测试
What if win11 can't pause the update? Win11 pause update is gray. How to solve it?
RichView RichEdit SRichViewEdit PageSize 页面设置与同步
随机推荐
How to create a pyramid with openmesh
架构师毕业总结
#yyds干货盘点#SQL聚合查询方法总结
Keras机器翻译实战
网上开户是安全的吗?新手可以开炒股账户吗。
渗透工具-TrustedSec 公司的渗透测试框架 (PTF)
天气预报小程序源码 天气类微信小程序源码
深度学习 神经网络基础
RichView RichEdit SRichViewEdit PageSize 页面设置与同步
【let var const】
Develop those things: easycvr cluster device management page function display optimization
2022/6/8-2022/6/12
图片拼图微信小程序源码_支持多模板制作和流量主
[mysql] install mysql5.7
如何用OpenMesh创建一个四棱锥
Win11暂停更新点不了怎么办?Win11暂停更新是灰色的如何解决?
Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
Learn white box test case design from simple to deep
internship:逐渐迈向项目开发
Détection des cibles - série Yolo