当前位置:网站首页>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);
}
}
}
}
边栏推荐
- [2022 actf] Web Topic recurrence
- [experience sharing] how to expand the cloud service icon for Visio
- The charm of SQL optimization! From 30248s to 0.001s
- 2022 welder (elementary) judgment questions and online simulation examination
- [CV] Wu Enda machine learning course notes | Chapter 8
- pytest+allure+jenkins安装问题:pytest: error: unrecognized arguments: --alluredir
- [Stanford Jiwang cs144 project] lab4: tcpconnection
- JSON data flattening pd json_ normalize
- PHP exports millions of data
- Value sequence (subsequence contribution problem)
猜你喜欢
Kbu1510-asemi power supply special 15A rectifier bridge kbu1510
2022年茶艺师(中级)考试试题及模拟考试
padavan手动安装php
SQL优化的魅力!从 30248s 到 0.001s
开源生态|打造活力开源社区,共建开源新生态!
[webrtc] M98 screen and window acquisition
A bit of knowledge - about Apple Certified MFI
Introduction to basic components of wechat applet
Figure out the working principle of gpt3
Few shot Learning & meta learning: small sample learning principle and Siamese network structure (I)
随机推荐
Value sequence (subsequence contribution problem)
pytest+allure+jenkins安装问题:pytest: error: unrecognized arguments: --alluredir
Problem solving: unable to connect to redis
Resource create package method
Pytorch parameter initialization
Wx is used in wechat applet Showtoast() for interface interaction
【VHDL 并行语句执行】
You Li takes you to talk about C language 6 (common keywords)
芯片 設計資料下載
A bit of knowledge - about Apple Certified MFI
Solve could not find or load the QT platform plugin "xcb" in "
【webrtc】m98 screen和window采集
C语言通信行程卡后台系统
Wechat applet data binding multiple data
nacos
【经验分享】如何为visio扩展云服务图标
Qt学习27 应用程序中的主窗口
芯片 设计资料下载
What are the positions of communication equipment manufacturers?
[VHDL parallel statement execution]