当前位置:网站首页>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);
}
边栏推荐
- Xshell 7 (SSH Remote Terminal tool) v7.0.0109 official Chinese Version (with file + installation tutorial)
- Short video with goods source code, save pictures to photo album / Gallery
- Mybaits: common database operations (Neusoft operations)
- 测试组的任务职责和测试的基本概念
- AD20/Altium designer——过孔盖油
- Service or mapper cannot be injected into a multithread
- JSP learning (I) -- overview of JSP
- Definition of thinking
- Which platform is safer to buy stocks on?
- 云端极简部署Svelte3聊天室
猜你喜欢

Mybaits:接口代理方式实现Dao

Come to Xiamen! Online communication quota free registration

clickhouse 21.x 集群四分片一副本部署

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

math_ Angular function & inverse trigonometric function

.NET 发布和支持计划介绍
![[face recognition] matlab simulation of face recognition based on googlenet deep learning network](/img/e8/050ca85542ccbf1402b84c5dbf6f5e.png)
[face recognition] matlab simulation of face recognition based on googlenet deep learning network

You call this crap high availability?

High voltage direct current (HVDC) model based on converter (MMC) technology and voltage source converter (VSC) (implemented by MATLAB & Simulink)

math_角函数&反三角函数
随机推荐
math_角函数&反三角函数
Xshell 7 (SSH Remote Terminal tool) v7.0.0109 official Chinese Version (with file + installation tutorial)
It may be the most comprehensive Matplotlib visualization tutorial in the whole network
同花顺软件是什么?手机开户安全么?
When online and offline integration accelerates and information docking channels are diversified, the traditional center will not be necessary
This article takes you to master the use of tcpdump command
MySQL instruction executes SQL file
0 basic how to get started software testing, can you succeed in changing careers?
Graduation season · undergraduate graduation thoughts -- the self-help road of mechanical er
无心剑中文随感《探求真谛》
[applet project development -- Jingdong Mall] configuration tabbar & window style for uni app development
缺失值處理
[mysql] data synchronization prompt: specified key was too long; max key length is 767 bytes
【人脸识别】基于GoogleNet深度学习网络的人脸识别matlab仿真
##Kibana+ELK集群日志处理
Quickly master asp Net authentication framework identity - login and logout
如何理解volatile
东华大学|具有强化知识感知推理的可解释推荐微观行为研究
Nuxt - 超详细环境搭建及创建项目整体流程(create-nuxt-app)
[small program project development -- Jingdong Mall] rotation chart of uni app development