当前位置:网站首页>力扣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
边栏推荐
- Mongodb learning
- Static keyword
- [FPGA] IIC读写EEPROM 的实现
- 一条命令运行rancher
- 【Paper】2016_ A Learning-Based Fault Tolerant Tracking Control of an Unmanned Quadrotor Helicopter
- Lambda&Stream
- Intern method of string
- Why does the computer have no network after win10 is turned on?
- Difference between TCP three handshakes and four waves and tcp/udp
- Singapore parent-child tour, these popular attractions must be arranged
猜你喜欢

Wildcard SSL certificate issuing time

Singapore parent-child tour, these popular attractions must be arranged

Network high concurrency

为什么win10开热点后电脑没有网络?

Meet in Bangkok for a romantic trip on Valentine's Day

Qos(Quality of Service)

Intern method of string

【Paper】2021_ Observer-Based Controllers for Incrementally Quadratic Nonlinear Systems With Disturbanc

Unreal 4 learning notes - set player birth point

Learn about threads
随机推荐
Webots learning notes
Salary management system based on servlet+jsp+mysql [source code + database]
This connection is not a private connection this website may be pretending to steal your personal or financial information
A collection of errors encountered in machine learning with unity
QT creator 8 beta2 release
Introduction to some representations, neighbors and degrees of Graphs
Bean创建流程 与 lazy-init 延迟加载机制原理
Redis实现短信登入功能(一)传统的Session登入
Deep learning ----- different methods to realize inception-10
Introduction to system programming
Brew install NVM command not found solution
Network high concurrency
Difference between request forwarding and redirection
Threejs realizes the simulation of river, surface flow, pipe flow and sea surface
How to apply for SSL certificate from the manufacturer
Create thread in callable mode
Method of applying for code signing certificate by enterprise
The most comprehensive summary notes of redis foundation + advanced project in history
brew安装nvm报nvm command not found解决方案
Enter the date format string as the production date of the commodity, and enter the shelf life (days); Calculate the number of days until today before the expiration date of the product. 1. Change the