Binary tree brushing practice (3)


foreword

insert image description here


Inheriting the previous writing ideas, this article continues the oj question of the binary tree

1. The minimum depth of a binary tree

1. Topic introduction

The topic is the minimum depth of the binary tree
insert image description here

2. Ideas

The minimum depth is the number of nodes on the shortest path from the root node to the nearest leaf node. A leaf node is a node without child nodes. Is it possible for a leaf node to appear only after a word count has been traversed? So for this question we will It can be converted into a shortest path to traverse to the leaves, then we will have the following situation, in this case, count 5, that is, return the minimum depth of the right subtreeinsert image description here
insert image description here

So we can summarize the following points

1. If the root is empty, return 0;
2. If the left node is empty, then directly return the minimum depth of the right node + 1 3.
If the left node is empty, then directly return the minimum depth of the right node + 1 But
4. If both are not empty, we return the smaller node between the left node and the right node + 1

3. Code

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
int minDepth(struct TreeNode* root){
    
    
    if(root==NULL)
    return 0;
    if(root->left==NULL)
    return minDepth(root->right)+1;
    else if(root->right==NULL)
    return minDepth(root->left)+1;
    int left=minDepth(root->left);
    int right=minDepth(root->right);
    if(left<right)
    {
    
    
        return left+1;
    }
    else
    {
    
    
        return right+1;
    }

}

Second, the number of nodes in a complete binary tree

1. Topic introduction

The title is about the number of nodes in a complete binary tree
insert image description here

2. Ideas

This question is about layer sequence traversal. Just traverse the binary tree layer sequence in this lesson. Layer sequence is to build a queue. Using the first-in-first-out nature of the queue, every time a node is popped, it will be brought into the left of the non-NULL node in turn. Node and right node, as shown in the figure below,
insert image description here
you can simulate the queue with an array, or you can directly use the code of the queue to make it

3. Code

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
typedef struct TreeNode* QDatatype;
typedef struct QueueNode
{
    
    
	struct QueueNode* next;
	QDatatype Data;
}QNode;

typedef struct Queue
{
    
    
	QNode* head;
	QNode* tail;
	int size;
}Queue;

void QueueInit(Queue* pq)
{
    
    
    assert(pq);
    pq->head = pq->tail = NULL;
    pq->size = 0;
}

void QueueDestroy(Queue* pq)
{
    
    
    assert(pq);
    QNode* cur = pq->head;
    while (cur)
    {
    
    
        QNode* next = cur->next;
        free(cur);
        cur = next;
    }
    pq->head = pq->tail = NULL;
    pq->size = 0;

}

void QueuePush(Queue* pq, QDatatype x)
{
    
    
    assert(pq);
    QNode* newnode = (QNode*)malloc(sizeof(QNode));
    assert(newnode);
    newnode->Data = x;
    newnode->next=NULL;
    if (pq->size == 0)
    {
    
    
        pq->head =pq->tail= newnode;
    }
    else
    {
    
    
        pq->tail->next = newnode;
        pq->tail = newnode;
    }
    pq->size++;

}

void QueuePop(Queue* pq)
{
    
    
    assert(pq);
    assert(pq->size);
    if (pq->head->next==NULL)
    {
    
    
        free(pq->head);
        pq->head =pq->tail= NULL;
    }
    else
    {
    
    
        QNode* next = pq->head->next;
        free(pq->head);
        pq->head = next;
    }
    pq->size--;
}

int QueueSize(Queue* pq)
{
    
    
    assert(pq);
    return pq->size;
}

bool QueueEmpty(Queue* pq)
{
    
    
    assert(pq);
    return pq->size==0;
}

QDatatype QueueFront(Queue* pq)
{
    
    
    assert(pq);
    assert(!QueueEmpty(pq));
    return pq->head->Data;
}

QDatatype QueueBack(Queue* pq)
{
    
    
    assert(pq);
    assert(!QueueEmpty(pq));
    return pq->tail->Data;
}




int countNodes(struct TreeNode* root){
    
    
    if(root==NULL)
    return 0;
	Queue tree;
	Queue* pt = &tree;
	QueueInit(pt);
	QueuePush(pt,root);
    int count=0;
	while (!QueueEmpty(pt))
	{
    
    
		struct TreeNode* front = QueueFront(pt);
        count ++;
		QueuePop(pt);
		if (front->left)
		{
    
    
			QueuePush(pt, front->left);
		}
		if (front->right)
		{
    
    
			QueuePush(pt, front->right);
		}
	}
	QueueDestroy(pt);
    return count;
}

Guess you like

Origin blog.csdn.net/Ruiren_/article/details/129996310