当前位置:网站首页>2022.06.23 (traversal of lc_144,94145\
2022.06.23 (traversal of lc_144,94145\
2022-06-24 09:08:00 【Leeli9316】

Method 1 : recursive
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
preorder(root, list);
return list;
}
public void preorder(TreeNode root, List<Integer> list) {
if (root == null) return;
list.add(root.val);
preorder(root.left, list);
preorder(root.right, list);
}
}Method 2 : iteration
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> ans = new ArrayList<>();
if (root == null) return ans;
Deque<TreeNode> stack = new LinkedList<>();
TreeNode node = root;
// Traversal termination conditions : The current node is empty and the stack is empty
while (node != null || !stack.isEmpty()) {
while (node != null) {
stack.push(node);
ans.add(node.val);
// Find the leftmost node
node = node.left;
}
node = stack.pop();
node = node.right;
}
return ans;
}
}边栏推荐
- Data middle office: overview of data governance
- 从华为WeAutomate数字机器人论坛,看政企领域的“政务新智理”
- Opencv maximum filtering (not limited to images)
- 陆奇:我现在最看好这四大技术趋势
- Array opposite pointer series
- The native applet uses canvas to make posters, which are scaled to the same scale. It is similar to the uniapp, but the writing method is a little different
- Digital cloud released the 2022 white paper on digital operation of global consumers in the beauty industry: global growth solves marketing problems
- 学习太极创客 — ESP8226 (十三)OTA
- Opencv daily function structure analysis and shape descriptor (7) finding polygon (contour) / rotating rectangle intersection
- Pytoch read data set (two modes: typical data set and user-defined data set)
猜你喜欢

普通人没有学历,自学编程可以月入过万吗?

Data middle office: detailed explanation of technical architecture of data middle office

Target detection series fast r-cnn

jupyter入门常见的几个坑:

从华为WeAutomate数字机器人论坛,看政企领域的“政务新智理”

Wan Weiwei, a researcher from Osaka University, Japan, introduced the rapid integration method and application of robot based on WRS system

"I can't understand Sudoku, so I choose to play Sudoku."

Opencv maximum filtering (not limited to images)

玄铁E906移植----番外0:玄铁C906仿真环境搭建

MBA-day25 最值问题-应用题
随机推荐
Qingcloud based R & D cloud solution for geographic information enterprises
Digital cloud released the 2022 white paper on digital operation of global consumers in the beauty industry: global growth solves marketing problems
嵌入式 | 硬件转软件的几条建议
Squid proxy application
[redis implements seckill business ①] seckill process overview | basic business implementation
MySQL | store notes of Master Kong MySQL from introduction to advanced
Data middle office: the data middle office practice scheme of Minsheng Bank
解决:jmeter5.5在win11下界面上的字特别小
【LeetCode】387. First unique character in string
Every (), map (), forearch () methods. There are objects in the array
华为路由器:GRE技术
Leetcode -- wrong set
4275. Dijkstra序列
Ebanb B1 Bracelet brush firmware abnormal interrupt handling
MySQL | 存储《康师傅MySQL从入门到高级》笔记
Pytoch read data set (two modes: typical data set and user-defined data set)
JS to find and update the specified value in the object through the key
EasyExcel单sheet页与多sheet页写出
Redis implements a globally unique ID
tcpdump抓包实现过程