当前位置:网站首页>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;
}
}边栏推荐
- jupyter入门常见的几个坑:
- 快慢指针系列
- "I can't understand Sudoku, so I choose to play Sudoku."
- Mba-day25 best value problem - application problem
- 【LeetCode】541. 反转字符串 II
- MySQL - SQL statement
- cookie加密 4 rpc方法确定cookie加密
- Webrtc series - network transmission 5: select the optimal connection switching
- 【LeetCode】415. 字符串相加
- Squid代理服务器应用
猜你喜欢

MySQL | view notes on Master Kong MySQL from introduction to advanced

Squid proxy application

【Redis實現秒殺業務①】秒殺流程概述|基本業務實現
![[pytoch basic tutorial 31] youtubednn model analysis](/img/18/dbeab69894583f6e5230772ce44652.png)
[pytoch basic tutorial 31] youtubednn model analysis

How to configure environment variables and distinguish environment packaging for multi terminal project of uniapp development

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

【LeetCode】387. First unique character in string

Spark - the number of leftouterjoin results is inconsistent with that of the left table

Matlab camera calibrator camera calibration

What is graph neural network? Figure what is the use of neural networks?
随机推荐
Qingcloud based "real estate integration" cloud solution
Prompt code when MySQL inserts Chinese data due to character set problems: 1366
4274. 后缀表达式
Wan Weiwei, a researcher from Osaka University, Japan, introduced the rapid integration method and application of robot based on WRS system
【LeetCode】415. 字符串相加
解决:模型训练时loss出现nan
Kaformer personal notes
KaFormer个人笔记整理
12、 Demonstration of all function realization effects
The printed object is [object object]. Solution
Qingcloud based R & D cloud solution for geographic information enterprises
[use picgo+ Tencent cloud object to store cos as a map bed]
Sword finger offer 55 - I. depth DFS method of binary tree
【Redis实现秒杀业务①】秒杀流程概述|基本业务实现
Idea another line shortcut
【MySQL从入门到精通】【高级篇】(一)字符集的修改与底层原理
Target detection series fast r-cnn
双指针模拟
Get post: do you really know the difference between requests??????
Solution: Nan occurs in loss during model training