当前位置:网站首页>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
边栏推荐
- [UAV] gyroscope data analysis, taking Victor intelligent jy901b as an example
- Sailing experience not to be missed in New York Tourism: take you to enjoy the magnificent city scenery from different perspectives
- Moore Manor diary I: realize the reclamation, sowing, watering and harvest in Moore Manor
- Differences between cookies and sessions
- Unreal 4 learning notes - set player birth point
- SSL update method
- Serializable and Deserialize
- One interview question a day the difference between B tree and b+ tree
- Sectigo certificate
- Easyrecovery data recovery software recovers my photo and video data two years ago
猜你喜欢

Connect to the database and run node JS running database shows that the database is missing

Interprocess communication

Create transfer generation point

Yolov5 torch installation

File and IO

Network layer protocol hardware

BeanFactory创建流程

How to repair expired SSL certificates?

Detailed explanation of the process of "flyingbird" small game (camera adjustment and following part)
![[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
随机推荐
深度学习------不同方法实现Inception-10
Universal Studios Singapore: a good place for a one-day parent-child tour in Singapore
Paging query, using jdbc-- paging query
Unreal 4 learning notes - Animated Montage
Why does the computer have no network after win10 is turned on?
Steamvr causes abnormal scene camera
Enlist soldiers and generals, draw small programs, multi-threaded display time
0 foundation starts self-study unit notes control direction becomes larger
Transport layer protocol tcp/udp
What is SQL injection and how to avoid it?
Unity enables simple music visualization
MySQL查询小工具(一)json格式的字符串字段中,替换json数组中对象的某个属性值
Servlet lifecycle
Unity is associated with vs. there is a compiler problem when opening
0 basic unity course. Bricklaying
史上最全的Redis基础+进阶项目实战总结笔记
EasyRecovery数据恢复软件 恢复了我两年前的照片视频数据
【Paper】2019_ Distributed Cooperative Control of a High-speed Train
力扣27. 移除元素
【Paper】2021_ Analysis of the Consensus Protocol of Heterogeneous Agents with Time-Delays