当前位置:网站首页>力扣589:N 叉树的前序遍历
力扣589:N 叉树的前序遍历
2022-06-30 04:45:00 【轩辕龙儿】
2022年03月10日 力扣每日一题
题目
给定一个 n 叉树的根节点 root ,返回 其节点值的 前序遍历 。
n 叉树 在输入中按层序遍历进行序列化表示,每组子节点由空值 null 分隔(请参见示例)。
示例 1:

输入:root = [1,null,3,2,4,null,5,6] 输出:[1,3,5,6,2,4]
示例 2:

输入:root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14] 输出:[1,2,3,6,7,11,14,4,8,12,5,9,13,10]
提示:
- 节点总数在范围
[0, 104]内 0 <= Node.val <= 104- n 叉树的高度小于或等于
1000
进阶:递归法很简单,你可以使用迭代法完成此题吗?
- 栈
- 树
- 深度优先搜索
个人解法
java代码:
class Solution {
List<Integer> list = new ArrayList<>();
public List<Integer> preorder(Node root) {
dfs(root);
return list;
}
void dfs(Node root) {
if (root == null) {
return;
}
list.add(root.val);
for (Node node : root.children) {
dfs(node);
}
}
}
python3代码:
""" # Definition for a Node. class Node: def __init__(self, val=None, children=None): self.val = val self.children = children """
from typing import List
class Solution:
def preorder(self, root: 'Node') -> List[int]:
result = []
def dfs(node):
if node:
result.append(node.val)
for child in node.children:
dfs(child)
dfs(root)
return result
边栏推荐
- EasyRecovery数据恢复软件 恢复了我两年前的照片视频数据
- Create a simple battle game with photon pun
- Learn about threads
- Input / output and interrupt technology -- microcomputer Chapter 6 learning notes
- Lambda&Stream
- [FPGA] IIC读写EEPROM 的实现
- Geotrustov wildcard
- Approaching history, introduction to the London Guard Museum
- 史上最全的Redis基础+进阶项目实战总结笔记
- Learning about signals
猜你喜欢

深度学习------不同方法实现Inception-10

Window10 jar double click to run without response

Mongodb learning

PBR material: basic principle and simple fabrication
![[UAV] gyroscope data analysis, taking Victor intelligent jy901b as an example](/img/d7/7bf43437edb87b69cdc5ae858f44e1.jpg)
[UAV] gyroscope data analysis, taking Victor intelligent jy901b as an example

Transport layer protocol tcp/udp

PS1 Contemporary Art Center, Museum of modern art, New York

Have a heart beating Valentine's day in Singapore

Redis implements SMS login function (I) traditional session login

What to do when the alicloud SSL certificate expires
随机推荐
What is an optocoupler circuit and what should be paid attention to in actual use?
Unreal 4 learning notes - set player birth point
深度学习------不同方法实现Inception-10
Salary management system based on servlet+jsp+mysql [source code + database]
PS1 Contemporary Art Center, Museum of modern art, New York
【Paper】2015_ Coordinated cruise control for high-speed train movements based on a multi-agent model
Cheap SSL certificate abroad
Collective system
Implementation of one interview question one distributed lock every day
Serializable and Deserialize
Thread safety and processing caused by multithreading
Have a heart beating Valentine's day in Singapore
IO stream, byte stream read / write copy
The role of break
The difference between get and post requests
One interview question and one joint index every day
[UAV] gyroscope data analysis, taking Victor intelligent jy901b as an example
Encapsulating JDBC tool classes
Mongodb learning
Yolov5 torch installation