当前位置:网站首页>Preorder traversal of Li Kou 589:n fork tree
Preorder traversal of Li Kou 589:n fork tree
2022-06-30 04:50:00 【Xuanyuan longer】
2022 year 03 month 10 Japan Try to make a daily question
subject
Given a n The root node of the fork tree root , return Of its node value The former sequence traversal .
n Fork tree In the input sequence traversal serialization representation , Each group of child nodes consists of null values null Separate ( See example ).
Example 1:

Input :root = [1,null,3,2,4,null,5,6] Output :[1,3,5,6,2,4]
Example 2:

Input :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] Output :[1,2,3,6,7,11,14,4,8,12,5,9,13,10]
Tips :
- The total number of nodes is in the range
[0, 104]Inside 0 <= Node.val <= 104- n The height of the fork tree is less than or equal to
1000
Advanced : Recursion is very simple , Can you use the iteration method to complete this problem ?
- Stack
- Trees
- Depth-first search
Personal solution
java Code :
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 Code :
""" # 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
边栏推荐
- 力扣209. 长度最小的子数组
- A collection of errors encountered in machine learning with unity
- One interview question a day the difference between B tree and b+ tree
- Arsenal Stadium Tour - take you to the front and back of Arsenal Stadium
- 力扣349. 两个数组的交集
- The most comprehensive summary notes of redis foundation + advanced project in history
- 【Paper】2015_ Active fault-tolerant control system design with trajectory re-planning against actuator
- 力扣27. 移除元素
- Redis实现短信登入功能(二)Redis实现登入功能
- IO stream, byte stream read / write copy
猜你喜欢

Free travel recommendation in Bangkok: introduction to the Mekong River in Bangkok

Wildcard SSL certificate issuing time

Unreal 4 learning notes - set player birth point

A collection of errors encountered in machine learning with unity

Collective system

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

【Paper】2015_ Active fault-tolerant control system design with trajectory re-planning against actuator

Break through the existing customer group marketing, and try customer grouping management (including clustering model and other practical effect evaluation)

一条命令运行rancher

harbor api 2.0查询
随机推荐
Introduction to some representations, neighbors and degrees of Graphs
Differences between cookies and sessions
Dual domain SSL certificate
Output directory of log files after unity3d packaging
0 foundation starts self-study unit notes control direction becomes larger
The subsystem implementing transaction persistence in DBMS is ()
Easyrecovery data recovery software recovers my photo and video data two years ago
Using the command line to convert JSON to dart file in fluent
PS1 Contemporary Art Center, Museum of modern art, New York
2021-03-16
Unity is associated with vs. there is a compiler problem when opening
力扣704. 二分查找
UE4 method of embedding web pages
Serializable and Deserialize
Yolov5 torch installation
【Paper】2019_ Consensus Control of Multiple AUVs Recovery System Under Switching Topologies and Time D
National Museum of Singapore - give you spiritual and physical satisfaction
【Paper】2015_ Active fault-tolerant control system design with trajectory re-planning against actuator
[control] multi agent system summary. 1. system model. 2. control objectives. 3. model transformation.
brew安装nvm报nvm command not found解决方案