当前位置:网站首页>Force buckle 144 Preorder traversal of binary tree
Force buckle 144 Preorder traversal of binary tree
2022-07-07 07:59:00 【Yangshiwei....】
subject :

analysis :
Preorder traversal is to first self node , Then the left node , Last right node , We can write a recursive function to realize .
Code :
/**
* 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 List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list =new ArrayList();
first(root,list);
return list;
}
public void first(TreeNode node,List<Integer> list){
if(node!=null){
list.add(node.val);
if(node.left!=null){
first(node.left,list);
}if(node.right!=null){
first(node.right,list);
}
}
}
}边栏推荐
- 即刻报名|飞桨黑客马拉松第三期等你挑战
- Route jump in wechat applet
- Explore Cassandra's decentralized distributed architecture
- IO stream file
- 3D reconstruction - stereo correction
- [advanced digital IC Verification] command query method and common command interpretation of VCs tool
- 2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions
- 2022焊工(初级)判断题及在线模拟考试
- Detailed explanation of Kalman filter for motion state estimation
- Asemi rectifier bridge rs210 parameters, rs210 specifications, rs210 package
猜你喜欢

探索干货篇!Apifox 建设思路

Detailed explanation of Kalman filter for motion state estimation

【经验分享】如何为visio扩展云服务图标

Custom class loader loads network class

Linux server development, MySQL transaction principle analysis

2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions

Cnopendata American Golden Globe Award winning data

2022 welder (elementary) judgment questions and online simulation examination

buuctf misc USB

mysql多列索引(组合索引)特点和使用场景
随机推荐
What are the positions of communication equipment manufacturers?
Ansible
SQL优化的魅力!从 30248s 到 0.001s
Leetcode 90: subset II
【p2p】本地抓包
You Li takes you to talk about C language 6 (common keywords)
快速使用 Jacoco 代码覆盖率统计
Numbers that appear only once
Is the test cycle compressed? Teach you 9 ways to deal with it
nacos
Redis technology leak detection and filling (II) - expired deletion strategy
Cnopendata American Golden Globe Award winning data
The principle and implementation of buffer playback of large video files
[guess-ctf2019] fake compressed packets
图解GPT3的工作原理
开源生态|打造活力开源社区,共建开源新生态!
The configuration that needs to be modified when switching between high and low versions of MySQL 5-8 (take aicode as an example here)
[webrtc] m98 Screen and Window Collection
misc ez_ usb
2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions