当前位置:网站首页>Pre order, middle order and post order traversal of tree
Pre order, middle order and post order traversal of tree
2022-06-12 12:12:00 【Star field_ 03zhan】
1. Use a collection to store traversal elements .
The former sequence traversal :( root – Left – Right )
public int preTraversal(TreeNode root){
List<Integer> list=new ArrayList<>();
helper(root,list);
return list;
}
private void helper(TreeNode root,List<Integer> list){
if(root == null) return;
list.add(root.val);
if(root.left != null) helper(root.left,list);
if(root.right != null) hepler(root.right,list);
}
In the sequence traversal
public int preTraversal(TreeNode root){
List<Integer> list=new ArrayList<>();
helper(root,list);
return list;
}
private void helper(TreeNode root,List<Integer> list){
if(root == null) return;
if(root.left != null) helper(root.left,list);
list.add(root.val);
if(root.right != null) hepler(root.right,list);
}
After the sequence traversal :
public int preTraversal(TreeNode root){
List<Integer> list=new ArrayList<>();
helper(root,list);
return list;
}
private void helper(TreeNode root,List<Integer> list){
if(root == null) return;
if(root.left != null) helper(root.left,list);
if(root.right != null) hepler(root.right,list);
list.add(root.val);
}
Reconstruction of binary tree :
According to the result of the preorder and inorder traversal of a binary tree , Build a binary tree and return its root node .
The idea of divide and conquer algorithm : A scale of N The problem is broken down into k A small-scale problem .
1. Establish root node
2. Construct left and right subtrees
Divide the left and right subtrees according to the root node .
How to divide the left and right subtrees ?
According to the left subtree / The root node of the right subtree 、 Left boundary 、 Right boundary to recursively construct left and right subtrees .
Create a hash table first dic The mapping of the values and indexes traversed in the storage .
HashMap<Integer,Integer> dic=new HashMap<>();
边栏推荐
- 什么是模块化?模块化的好处
- [transfer]placement NEW
- #ifndef#define#endif防止头文件重复包含, 你不是真的懂
- Elk construction guide
- 机器学习之线性模型
- Redis的主从复制原理
- Promise controls the number of concurrent requests
- Ficusjs series (I) introduction to ficusjs
- 获取本机所有ipv4, ipv6地址
- How to operate the newly revised Taobao merchants and what should be paid attention to
猜你喜欢

Open source project - (erp+ Hotel + e-commerce) background management system

5g NR Protocol Learning - - ts38.211 downlink channel

转载--win10打开任务管理器就蓝屏的问题

TinyMCE series (II) TinyMCE plug-in development

In navigation, the solution of adding borders to affect the layout

PDSCH 相关

创建Servlet项目

开源项目-(ERP+酒店+电商)后台管理系统

Redis的主从复制原理

Longest string without duplicate characters (leetcode 3)
随机推荐
异步路径处理
TinyMCE series (III) introduction to common TinyMCE APIs
imx6-uboot添加lvds1显示
Autolock solves the problem of forgetting to unlock after locking
The difference between bind, call and apply, and the encapsulation of bind()
左右案例+小圆点的轮播图+无时间
Win7 registers out of process components, services, and COM component debugging
Neighbor item status update of neighbor subsystem
Asynchronous path processing
Create servlet project
Backtracking, eight queens
[foundation of deep learning] back propagation method (1)
win7注册进程外组件, 服务, 以及COM组件调试
[译] QUIC Wire Layout Specification - Packet Types and Formats | QUIC协议标准中文翻译(2) 包类型和格式
Google Earth engine (GEE) - quick land classification by kmeans clustering (double for loop quick parameter adjustment)
Shielding does not display vs warning
你不会只会用console.log()吧?
Beyondcompare 4 uses PJ
ACE配置IPv6, VS静态编译ACE库
MVC模式、加密、jsonwebtoken