当前位置:网站首页>Summary of binary tree recursive routines
Summary of binary tree recursive routines
2022-07-05 23:07:00 【Bright morning light】
The time complexity of all previous binary tree recursion routines is O ( N ) O(N) O(N), among N N N Is the number of nodes in a binary tree , And they are all optimal solutions .
A node gets information from left and right children , Then integrate into this node , At this time, the left and right children are no longer needed , The parent node of this node also repeats this process , The whole process is equivalent to After the sequence traversal , Do dynamic planning on the tree , So it's called tree dp.
This routine includes two levels :
Thought reminder
How to achieve the goal :X How to achieve the goal of a binary tree with root node ?
Means of implementation : Ask for information from the left and right trees , List the possibilities , This possibility must be based on the information from the left and right trees ( Constant time can be obtained ), The processed information of the same magnitude is also constant time .
Design information body , Analyze possibilities , The common classification is “ And target X of ” and “ And target X irrelevant ”.Templates
① DesignInfoInformation body ;
②processfunction :processFunction must returnInfo; When empty , If the value is bad, it will return null , Set it if it is good ;
③ By default, the left tree information is obtained first , Then get the right tree information ;
④ After analyzing the possibility , hold X All the necessary information is processed , This is highly templated .
This routine can solve most binary tree problems, especially tree type dp problem , The essence is to use Recursively traversing a binary tree The convenience of .
The whole process is summarized as follows :
1) Suppose X Node as root , Suppose you can ask X Left tree and X The right tree wants any information
2) Under the assumptions of the previous step , Discuss with X Is the tree of the root node , Got the answer possibility ( above all )
3) After listing all the possibilities , Determine what kind of information you need from the left tree and the right tree
4) Find the complete set of left tree information and right tree information , Is the information that any subtree needs to return S
5) Recursive functions return S, Every sub tree asks so
6) Write code , In the code, consider how to integrate the information of the left tree and the information of the right tree into the information of the whole tree
边栏推荐
- First, redis summarizes the installation types
- Use of metadata in golang grpc
- Sum of two numbers, sum of three numbers (sort + double pointer)
- 一文搞定class的微觀結構和指令
- 判断二叉树是否为完全二叉树
- 使用rewrite规则实现将所有到a域名的访问rewrite到b域名
- Ieventsystemhandler event interface
- Codeforces Global Round 19
- 3D reconstruction of point cloud
- Spectrum analysis of ADC sampling sequence based on stm32
猜你喜欢

Arduino measures AC current

Using LNMP to build WordPress sites

Hainan Nuanshen tea recruits warmhearted people: recruitment of the product experience recommender of Nuanshen multi bubble honey orchid single cluster

Google Maps case

openresty ngx_ Lua request response

audiopolicy

一文搞定JVM的内存结构

如何快速理解复杂业务,系统思考问题?

audiopolicy

Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
随机推荐
Use of metadata in golang grpc
[digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]
从 1.5 开始搭建一个微服务框架——日志追踪 traceId
openresty ngx_lua请求响应
d3dx9_ How to repair 31.dll_ d3dx9_ 31. Solution to missing DLL
Déterminer si un arbre binaire est un arbre binaire complet
Global and Chinese markets of industrial pH meters 2022-2028: Research Report on technology, participants, trends, market size and share
Ieventsystemhandler event interface
513. Find the value in the lower left corner of the tree
LeetCode145. Post order traversal of binary tree (three methods of recursion and iteration)
透彻理解JVM类加载子系统
判断二叉树是否为完全二叉树
Use of grpc interceptor
第十七周作业
30 optimization skills about mysql, super practical
6-axis and 9-axis IMU attitude estimation
一文搞定JVM常见工具和优化策略
Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
Arduino 测量交流电流
Go语言实现原理——Map实现原理