当前位置:网站首页>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);
}
}

边栏推荐
- [QT] test whether QT can connect to the database
- 牛客-练习赛101-推理小丑
- [QT] solve the problem that QT MSVC 2017 cannot compile
- 449-原码、补码、反码
- What is ThreadLocal memory leak and how to solve it
- 九州云与英特尔联合发布智慧校园私有云框架,赋能教育新基建
- 【CMake】Qt creator 里面的 cmake 配置
- 启牛学院开户安全的吗?开户怎么开?
- ThreadLocal内存泄漏是什么,怎么解决
- 2023 Lexus ES products have been announced, which makes great progress this time
猜你喜欢

Qt5.12.9 migration tutorial based on Quanzhi H3

LDR6035智能蓝牙音响可充可放(5.9.12.15.20V)快充快放设备充电

Intelligent operation and maintenance practice: banking business process and single transaction tracking

How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03

B tree and b+tree of MySQL

SQL Server 安装指南

时间复杂度与空间复杂度

Key points of security agreement

What is ThreadLocal memory leak and how to solve it

Windows installation WSL (II)
随机推荐
Node - generate wechat permission verification configuration
MySQL: the difference between insert ignore, insert and replace
Key points of security agreement
Material design component - use bottomsheet to show extended content (I)
How can entrepreneurial teams implement agile testing to improve quality and efficiency? Voice network developer entrepreneurship lecture Vol.03
挖财学堂开户打新债安全可靠嘛?
4. Object mapping Mapstercover
Barbie q! How to analyze the new game app?
下载在线视频 m3u8使用教程
记录一下大文件上传偶然成功偶然失败问题
The origin of usb-if Association and various interfaces
【mysql 07】GPG key retrieval failed: “Couldn‘t open file /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql-2022“
What does open loop and closed loop mean?
【QT】对于Qt MSVC 2017无法编译的问题解决
【QT】QtCreator卸载与安装(非正常状态)
USB-IF协会与各种接口的由来
微信小程序缓存过期时间的相关设置(推荐)
Record the accidental success and failure of uploading large files
Linux CentOS7安装Oracle11g的超完美新手教程
PHP reads ini or env type configuration