当前位置:网站首页>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);
}
}
}
}
边栏推荐
- Rust versus go (which is my preferred language?)
- 这5个摸鱼神器太火了!程序员:知道了快删!
- The charm of SQL optimization! From 30248s to 0.001s
- Common method signatures and meanings of Iterable, collection and list
- Live online system source code, using valueanimator to achieve view zoom in and out animation effect
- Introduction to basic components of wechat applet
- Linux server development, detailed explanation of redis related commands and their principles
- 2022制冷与空调设备运行操作复训题库及答案
- [guess-ctf2019] fake compressed packets
- 芯片资料 网站 易特创芯
猜你喜欢
解决问题:Unable to connect to Redis
[Stanford Jiwang cs144 project] lab4: tcpconnection
Sign up now | oar hacker marathon phase III, waiting for your challenge
[CV] Wu Enda machine learning course notes | Chapter 8
【经验分享】如何为visio扩展云服务图标
2022制冷与空调设备运行操作复训题库及答案
Numbers that appear only once
Introduction to basic components of wechat applet
2022 tea master (intermediate) examination questions and mock examination
3D reconstruction - stereo correction
随机推荐
[matlab] when matrix multiplication in Simulink user-defined function does not work properly, matrix multiplication module in module library can be used instead
[webrtc] m98 Screen and Window Collection
探索干货篇!Apifox 建设思路
Custom class loader loads network class
Linux server development, MySQL transaction principle analysis
Pytest+allure+jenkins environment -- completion of pit filling
pytest+allure+jenkins环境--填坑完毕
Detailed explanation of uboot image generation process of Hisilicon chip (hi3516dv300)
2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions
A bit of knowledge - about Apple Certified MFI
Force buckle 145 Binary Tree Postorder Traversal
2022-07-06: will the following go language codes be panic? A: Meeting; B: No. package main import “C“ func main() { var ch chan struct
【经验分享】如何为visio扩展云服务图标
Idea add class annotation template and method template
nacos
[VHDL parallel statement execution]
Operation suggestions for today's spot Silver
LeetCode 40:组合总和 II
Live broadcast platform source code, foldable menu bar
[OBS] win capture requires winrt