当前位置:网站首页>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
边栏推荐
- Event trigger requirements of the function called by the event trigger
- Thoroughly understand JVM class loading subsystem
- Realize reverse proxy client IP transparent transmission
- TypeError: this. getOptions is not a function
- Hcip day 11 (BGP agreement)
- 2:第一章:认识JVM规范1:JVM简介;
- openresty ngx_lua正則錶達式
- The maximum happiness of the party
- Finally understand what dynamic planning is
- Masked Autoencoders Are Scalable Vision Learners (MAE)
猜你喜欢

Week 17 homework

视频标准二三事

利用LNMP实现wordpress站点搭建

First, redis summarizes the installation types

Go语言实现原理——锁实现原理

一文搞定垃圾回收器
![[untitled]](/img/8c/607776e79d66acf9282dca127e12e1.jpg)
[untitled]

2022 G3 boiler water treatment simulation examination and G3 boiler water treatment simulation examination question bank

Activate function and its gradient

Codeforces Global Round 19
随机推荐
Media query: importing resources
3:第一章:认识JVM规范2:JVM规范,简介;
数学公式截图识别神器Mathpix无限使用教程
东南亚电商指南,卖家如何布局东南亚市场?
Three. Js-01 getting started
How to quickly understand complex businesses and systematically think about problems?
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
第十七周作业
Distributed solution selection
Finally understand what dynamic planning is
Leecode learning notes
Realize reverse proxy client IP transparent transmission
Vcomp110.dll download -vcomp110 What if DLL is lost
Function default parameters, function placeholder parameters, function overloading and precautions
Nail error code Encyclopedia
一文搞定垃圾回收器
Ieventsystemhandler event interface
Use of metadata in golang grpc
30 optimization skills about mysql, super practical
Google Maps case