当前位置:网站首页>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);
}
}
}
}边栏推荐
猜你喜欢

Custom class loader loads network class

Common method signatures and meanings of Iterable, collection and list
![[CV] Wu Enda machine learning course notes | Chapter 8](/img/c0/7a39355fb3a6cb506f0fbcf2a7aa24.jpg)
[CV] Wu Enda machine learning course notes | Chapter 8

2022 tea master (intermediate) examination questions and mock examination

Qt学习26 布局管理综合实例

json 数据展平pd.json_normalize

Resource create package method

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

Use and analysis of dot function in numpy

2022制冷与空调设备运行操作复训题库及答案
随机推荐
JSON data flattening pd json_ normalize
Codeforces Global Round 19
MySQL multi column index (composite index) features and usage scenarios
2022 National latest fire-fighting facility operator (primary fire-fighting facility operator) simulation questions and answers
misc ez_usb
pytest+allure+jenkins环境--填坑完毕
2022焊工(初级)判断题及在线模拟考试
[mathematical notes] radian
What are the positions of communication equipment manufacturers?
Use and analysis of dot function in numpy
图解GPT3的工作原理
Linux server development, MySQL stored procedures, functions and triggers
Detailed explanation of uboot image generation process of Hisilicon chip (hi3516dv300)
Live broadcast platform source code, foldable menu bar
[2022 ciscn] replay of preliminary web topics
2022 welder (elementary) judgment questions and online simulation examination
Sign up now | oar hacker marathon phase III, waiting for your challenge
Operation suggestions for today's spot Silver
Live online system source code, using valueanimator to achieve view zoom in and out animation effect
【数字IC验证快速入门】17、SystemVerilog学习之基本语法4(随机化Randomization)