当前位置:网站首页>Leetcode skimming: binary tree 01 (preorder traversal of binary tree)
Leetcode skimming: binary tree 01 (preorder traversal of binary tree)
2022-07-02 00:26:00 【Taotao can't learn English】
144. Preorder traversal of two tree
1. Determine the parameters and return values of recursive functions
2. Determine termination conditions
3. Determine the logic of monolayer recursion
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. Preorder traversal of two tree * https://leetcode.cn/problems/binary-tree-preorder-traversal/ **/
public class PreorderTraversal {
/** * The former sequence traversal , Root left and right . * @param root * @return */
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
traversal(root, result);
return result;
}
/** * The former sequence traversal , Root left and right . * * @param root * @param result * 1. Determine the parameters and return values of recursive functions * 2. Determine termination conditions * 3. Determine the logic of monolayer recursion */
public void traversal(TreeNode root, List<Integer> result) {
// Termination conditions
if (root == null) {
return;
}
// Access root
result.add(root.val);
// Visit the left subtree
traversal(root.left, result);
// Visit the right subtree
traversal(root.right,result);
}
}
边栏推荐
- MySQL: the difference between insert ignore, insert and replace
- 关联性——组内相关系数
- 2023款雷克萨斯ES产品公布,这回进步很有感
- I want to ask, which is the better choice for securities companies? I don't understand. Is it safe to open an account online now?
- SQL Server Installation Guide
- cookie、session、tooken
- Gaussdb (for MySQL):partial result cache, which accelerates the operator by caching intermediate results
- Database -- sqlserver details
- [embedded system course design] a single key controls the LED light
- Leetcode medium question sharing (5)
猜你喜欢
Flow control statement of SQL data analysis [if, case... When detailed]
Review data desensitization system
创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
leetcode96不同的二叉搜索树
Kyushu cloud and Intel jointly released the smart campus private cloud framework, enabling new infrastructure for education
Difficult to get up syndrome (bit by bit greed)
The new version of graphic network PDF will be released soon
Material design component - use bottomsheet to show extended content (I)
Leetcode medium question sharing (5)
SQL数据分析之子查询的综合用法和案例题【耐心整理】
随机推荐
【CTF】bjdctf_2020_babystack2
2023款雷克萨斯ES产品公布,这回进步很有感
SQL数据分析之窗口排序函数rank、dense_rank、raw_number与lag、lead窗口偏移函数【用法整理】
启牛商学院给的证券账户安不安全?哪里可以开户
mysql之B tree 以及 B+tree
2022 pinduoduo details / pinduoduo product details / pinduoduo SKU details
Learn online case practice
Dongge cashes in and the boss retires?
ERP项目施行计划的目的是什么?
Is it safe to choose mobile phone for stock trading account opening in Beijing?
449-原码、补码、反码
在证券账户上买基金安全吗?哪里可以买基金
Barbie q! How to analyze the new game app?
Which securities company is safer to open a stock account
【QT】Qt 使用MSVC2017找不到编译器的解决办法
启牛学院开户安全的吗?开户怎么开?
创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03
export default 导出的对象,不能解构问题,和module.exports的区别
Shell process control
九州云与英特尔联合发布智慧校园私有云框架,赋能教育新基建