当前位置:网站首页>Binary tree practice the second bullet
Binary tree practice the second bullet
2022-06-22 17:57:00 【bit_ dhdw】
Catalog
One : Binary tree node problem
(1) All nodes of binary tree
- To calculate the number of nodes in a binary tree, we can traverse the binary tree , If it is not empty, increase the size by one
- So we can write code like this :
void TreeSize(BTNode* root,int*size)// Number of all nodes
{
if (root == NULL)
{
return;
}
(*size)++;
TreeSize(root->left,size);
TreeSize(root->right,size);
return root == NULL ? 0 : TreeSize(root->left) + TreeSize(root->right) + 1;
}
- Preorder traversal binary tree . If the node is empty, directly return , Otherwise it would be size++
- But we can also use the idea of divide and conquer to calculate the number of nodes in the binary tree
- Total number of nodes = The number of nodes in the left subtree + Number of right subtree nodes +1( Plus one because the root is not empty , If the root is empty, return directly 0)
- So our code can be simplified into one line
int TreeSize(BTNode* root)
{
return root == NULL ? 0 : TreeSize(root->left) + TreeSize(root->right) + 1;
}
(2) The second fork is the tree k Layer nodes
Computation first k The number of layer nodes continues to use the divide and conquer idea
Number of roots k The number of nodes in the layer = Left subtree k-1 The number of nodes in the layer + Right subtree k-1 The number of nodes in the layer
When k==1 when , Number of nodes returned 1, If the node is empty, return 0

The code is as follows
int TreeKLevelSize(BTNode* root, int k)// The first k The number of layer nodes
{
if (root == NULL)
{
return 0;
}
if (k == 1)
{
return 1;
}
return TreeKLevelSize(root->left, k - 1) + TreeKLevelSize(root->right, k - 1);
}
(3) Binary tree leaf node
- Let's recall the definition of leaf node
- Degree is 0 The nodes of are called leaf nodes
- How to calculate the number of leaf nodes of the whole binary tree
- by the way , Namely The number of leaf nodes in the left subtree plus the number of leaf nodes in the right subtree
- Therefore, when determining whether a node is a leaf node, you need to determine whether both the left tree node and the right tree node are empty
- If the root is NULL, Just go back to 0
- If the left and right subtree nodes are NULL, This node is a leaf node , return 1
- If the above two conditions are not satisfied, the leaf node has not been found , Continue to recursive
- The code is as follows :
int TreeLeafSize(BTNode* root)// Number of leaf nodes
{
if (root == NULL)
{
return 0;
}
else
{
if (root->left == NULL && root->right ==NULL)
{
return 1;
}
else
{
return (TreeLeafSize(root->left) + TreeLeafSize(root->right));
}
}
}
Two : Find a binary tree node
- Finding a binary tree is slightly different from the previous writing , We need to create variables to hold our return values
- The idea is very simple , If the root is null, return null , If the value of the root is the same as the value found, the root is returned 、
- If not, look for the left subtree and the right subtree , Until an empty node is found
- And the node we found may be found very late , So you need to go back layer by layer , It is necessary to create variables to store the return value of each function call
- We create variables ret Store the return value of the left subtree , If it is not empty, it means you have found , Go straight back to ret, Don't look for the right subtree
- If ret If it is empty, we will continue to find the right subtree , And regardless of ret Why is the value returned directly
- Because if ret Returns directly for the value we find ret I found it , If not ret Namely NULL, What we finally return is NULL
BTNode* TreeFind(BTNode* root, BTDataType x)// Find a binary tree node
{
if (root == NULL)
{
return NULL;
}
if (root->data == x)
{
return root;
}
BTNode* ret=TreeFind(root->left, x);
if (ret != NULL)
{
return ret;
}
ret=TreeFind(root->right, x);
return ret;
}
3、 ... and : Flip binary tree
- To flip a binary tree is to swap the nodes of each left and right subtree
- Examples are as follows :

- The idea is not complicated , First create a new variable and save the right subtree , After that, the left subtree is flipped and assigned to the right subtree
- Then turn the right subtree over and assign it to the left subtree
- Eventually return root
BTNode* InvertTree(BTNode* root)// Flip binary tree
{
if (root == NULL)
{
return NULL;
}
BTNode* right = root->right;
root->right = InvertTree(root->left);
root->left = InvertTree(right);
return root;
}
Four : Judge whether two binary trees are equal
- If two binary trees are NULL, return true
- One for Null One is not for NULL, return false
- The values of the two roots are not equal , return false
- If none of the above is satisfied , Continue to judge whether the left subtree and the right subtree are equal
- Because the nodes of two binary trees are equal only when the left and right subtrees are equal , So you need to use && Connect
- Each node is the same, and then it returns true, If there is a difference, it will return false
bool isSameTree(BTNode* p, BTNode* q)// Judge whether two binary trees are equal
{
if (p == NULL && q == NULL)
{
return true;
}
if (p == NULL || q == NULL)
{
return false;
}
if (p->data != q->data)
{
return false;
}
return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
}
5、 ... and : Determine whether to subtree
- Let's assume that neither binary tree is empty
- To determine whether a tree is a subtree of another tree , You need to traverse the tree , Whether each subtree is equal to its comparison
- Just need to use the last function to judge whether two binary trees are equal
- First, if the root is empty , Neither tree is empty , It means that the mother tree has come to the end , Go straight back to false
- If the root is not empty, compare it to another tree , If the same, return directly to true
- The left subtree and the right subtree are used for comparison , Return as long as there is the same true
bool isSubtree(BTNode* root, BTNode* subRoot)
{
if (root == NULL)
{
return false;
}
if (isSameTree(root, subRoot) == true)
{
return true;
}
return isSubtree(root->left, subRoot) || isSubtree(root->right, subRoot);
}
边栏推荐
- [applet project development -- Jingdong Mall] subcontracting configuration of uni app development
- 数据库行业分析:从全球IT产业趋势到国产数据库发展之路
- client-go gin的简单整合十-Update
- UI automation positioning edge -xpath actual combat
- redis. clients. jedis. exceptions. JedisDataException ERR invalid password.
- SaaS化应用开发指南
- How to solve the problem of database?
- CMB model 23 ukey is not recognized on win7
- The principle of locality in big talk
- Docker之MySQL主从连接提示:Communications link failure
猜你喜欢
![[step 1 of advanced automated testing] 1 minute to introduce you to automated testing](/img/00/9647d552749092954a91bd84307773.png)
[step 1 of advanced automated testing] 1 minute to introduce you to automated testing

Activity启动流程梳理

MySQL master-slave connection prompt of docker: communications link failure

DAP事实表加工汇总功能应用说明

Mybaits:接口代理方式实现Dao

Noah fortune plans to land on the Hong Kong Stock Exchange: the performance fell sharply in the first quarter, and once stepped on the thunder "Chengxing case"

Gridhome, a must-have static site generator for beginners

GPIO operation method of imx6ull

math_ Angular function & inverse trigonometric function

Tried several report tools, and finally found a report based on Net 6
随机推荐
Docker之MySQL主从连接提示:Communications link failure
[mysql] install multiple MySQL versions on one Windows computer
Simple integration of client go gin -update
Hello Playwright:(7)模拟键盘和鼠标
Missing value handling
[applet project development -- Jingdong Mall] subcontracting configuration of uni app development
CMB model 23 ukey is not recognized on win7
内容推荐流程
It may be the most comprehensive Matplotlib visualization tutorial in the whole network
Which platform is safer to buy stocks on?
当线上线下的融合加速,当信息对接渠道的多样化,传统意义上的中心将没有必要
A classmate asked what framework PHP should learn?
STM32 series (HAL Library) - f103c8t6 hardware SPI illuminates OLED screen with word library
短视频直播源码,EditText输入框的使用
Recommend 7 super easy-to-use terminal tools - ssh+ftp
【FPGA+PWM】基于FPGA的三相PWM整流器移相触发电路的设计与实现
Content recommendation process
抢先报名丨新一代 HTAP 数据库如何在云上重塑?TiDB V6 线上发布会即将揭晓!
Is flush easy to open an account? Is it safe to open an account online?
基于转换器 (MMC) 技术和电压源转换器 (VSC) 的高压直流 (HVDC) 模型(Matlab&Simulink实现)