当前位置:网站首页>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
边栏推荐
- Shell: operator
- Global and Chinese market of water treatment technology 2022-2028: Research Report on technology, participants, trends, market size and share
- Alibaba Tianchi SQL training camp task4 learning notes
- Hcip day 11 (BGP agreement)
- Debian 10 installation configuration
- Registration and skills of hoisting machinery command examination in 2022
- Getting started stm32--gpio (running lantern) (nanny level)
- MoCo: Momentum Contrast for Unsupervised Visual Representation Learning
- 6-axis and 9-axis IMU attitude estimation
- Registration of Electrical Engineering (elementary) examination in 2022 and the latest analysis of Electrical Engineering (elementary)
猜你喜欢

Non rigid / flexible point cloud ICP registration

Paddle Serving v0.9.0 重磅发布多机多卡分布式推理框架

First, redis summarizes the installation types

Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~

数据库基础知识(面试)

基于STM32的ADC采样序列频谱分析

Tensor attribute statistics

Common model making instructions

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

Three. JS VR house viewing
随机推荐
Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
Media query: importing resources
3D reconstruction of point cloud
Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
从 1.5 开始搭建一个微服务框架——日志追踪 traceId
Nacos 的安装与服务的注册
Ultrasonic sensor flash | LEGO eV3 Teaching
东南亚电商指南,卖家如何布局东南亚市场?
Go语言实现原理——Map实现原理
Yiwen gets rid of the garbage collector
VIM tail head intercept file import
2022 G3 boiler water treatment simulation examination and G3 boiler water treatment simulation examination question bank
视频标准二三事
d3dx9_ What if 29.dll is missing? System missing d3dx9_ Solution of 29.dll file
audiopolicy
Nail error code Encyclopedia
Common model making instructions
Arduino measures AC current
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
Element operation and element waiting in Web Automation