当前位置:网站首页>144. Preorder traversal of binary tree
144. Preorder traversal of binary tree
2022-07-20 23:59:00 【Struggling young man】
144. Preorder traversal of two tree
Ideas :
In general, what binary tree pays attention to is a complete solution system and framework routine , Not many people really understand , Most people will be confused by the program during recursive traversal . But in fact, as long as you understand the difference between loop and recursion , Even if you understand the binary tree .
This problem is the preorder traversal of binary tree , On the force button, it is a simple question , So readers will probably understand my code and see the problem-solving framework .
The topic is to let the preorder traverse , Enter a binary tree , You output its preorder traversal result . This problem is nothing more than writing a traversal array (travser) Traverse the binary tree alone , At the same time, operate on the key nodes of traversal ( Preorder traversal position / Middle order traversal position / Postorder traversal position ), For example, this problem . When traversing to the preamble position , You should save the corresponding value of the node at this time into an array , Here I define a Arraylist Save the results .
/** * 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 {
ArrayList<Integer> res = new ArrayList<>();
public List<Integer> preorderTraversal(TreeNode root) {
travser(root);
return res;
}
void travser(TreeNode node){
if(node == null){
return ;
}
res.add(node.val);
travser(node.left);
travser(node.right);
}
}
边栏推荐
猜你喜欢
[ROS] handwritten message publisher and subscriber
ES6 - promise vs function "callback hell"
1235. 规划兼职工作 动态规划
When byte hung up, the interviewer asked DDD, but I didn't know
IDEA版Postman面世了,功能真心强大
Data center, a new type of infrastructure that determines the driving force of the city's future development
EL&JSTL:EL表达式总结
重新认识生命世界,也重新认识我们自己
将流转化为数据产品
openGauss社区理事长江大勇:openGauss联合产业界创新,共建开源数据库根社区
随机推荐
关于业务安全平台架构设计,顶象给“我”讲透了
Bubble sort
When byte hung up, the interviewer asked DDD, but I didn't know
医学 性别、年龄、受教育年限的校正 方法与代码
Adding, deleting, querying and modifying MySQL tables (II)
Kubernetes — client-go组件
for语句 range函数
[ROS] topic based use
Application of GIS technology in the medical industry: using slice map publishing technology to solve the online browsing of dmetrix digital pathological slices
移动端中的分辨率、视口与二倍图
RuntimeError: CUDA error: an illegal memory access was encountered
Is it safe to choose online stock account opening in Hangzhou and Wuhan?
1235. 规划兼职工作 动态规划
Revit (22) familyinstance common API encapsulation
Which platform is the best and safest to buy stocks on?
Helm — Chart介绍
[Axi] interpret the low-power design of Axi protocol
These super easy-to-use browser plug-ins are unknown to many people
[ROS] handwritten message publisher and subscriber
The idea version of postman has been released, and its functions are really powerful