"Leetcode" 222. The number of nodes in a complete binary tree

This article: https://github.com/youngyangyang04/leetcode-master already included, there is also the question Raiders leetcode brush, brush each type classic topic title sequence, mind map, you can fork into their own warehouses , empty look You will definitely gain something, if it helps you, give a star to support it!

If the previous two Binary Trees: Look at the maximum depth of these treesBinary Tree: Look at the minimum depth of these trees , if you read carefully, this topic can be solved in minutes!

222. Number of nodes in a complete binary tree

Given a complete binary tree, find the number of nodes in the tree.

Example:

 

 

Ideas

In fact, this question does not need to emphasize that it is a complete binary tree, that is, the number of binary tree nodes.

It can still be solved using recursion and iteration.

The recursive method of this topic is similar to the depth of the binary tree, while the iterative method: the binary tree sequence traversal template is slightly modified to record the number of nodes traversed.

The order of recursive traversal is still post-order (left and right).

Recursion

If you are not familiar with finding the depth of binary trees, read this article: Binary Trees: Look at the maximum depth of these trees .

  1. Determine the parameters and return value of the recursive function: the parameter is the root node of the incoming tree, and the number of nodes in the binary tree with this node as the root node will be returned, so the return value is of type int.

code show as below:

int getNodesNum(TreeNode* cur) {
  1. Determine the termination condition: If it is an empty node, return 0, which means that the number of nodes is 0.

code show as below:

if (cur == NULL) return 0;
  1. Determine the logic of single-level recursion: first find the number of nodes in its left subtree, then find the number of nodes in the right subtree, and finally take the sum and add one (adding 1 is because the current intermediate node is counted), the current node is the root The number of nodes of the node.

code show as below:

int leftNum = getNodesNum(cur->left);      // 左
int rightNum = getNodesNum(cur->right);    // 右
int treeNum = leftNum + rightNum + 1;      // 中
return treeNum;

So the overall C++ code is as follows:

class Solution {
private:
    int getNodesNum(TreeNode* cur) {
        if (cur == 0) return 0;
        int leftNum = getNodesNum(cur->left);      // 左
        int rightNum = getNodesNum(cur->right);    // 右
        int treeNum = leftNum + rightNum + 1;      // 中
        return treeNum;
    }
public:
    int countNodes(TreeNode* root) {
        return getNodesNum(root);
    }
};

After the code is simplified, the C++ code is as follows:

class Solution {
public:
    int countNodes(TreeNode* root) {
        if (root == NULL) return 0;
        return 1 + countNodes(root->left) + countNodes(root->right);
    }
};

Iterative method

If you are not familiar with seeking binary tree sequence traversal, read this article: Binary Tree: Sequence traversal is here! .

So as long as there are few changes to the template, add a variable result and count the number of nodes.

class Solution {
public:
    int countNodes(TreeNode* root) {
        queue<TreeNode*> que;
        if (root != NULL) que.push(root);
        int result = 0;
        while (!que.empty()) {
            int size = que.size();
            for (int i = 0; i < size; i++) {
                TreeNode* node = que.front();
                que.pop();
                result++;   // 记录节点数量
                if (node->left) que.push(node->left);
                if (node->right) que.push(node->right);
            }
        }
        return result;
    }
};

to sum up

The solution to this problem is actually in binary trees: look at the maximum depth of these trees and  binary trees: look at the minimum depth of these trees have been mentioned.

The same analysis routine, the code is similar, it is estimated that at this time, everyone should be very proficient in seeking the number of binary tree nodes and seeking depth.

Students who have never done this topic can happily brush it.

 

My B station (there are algorithm videos and programming related knowledge I explained) : https://space.bilibili.com/525438321

I am Carl, a programmer, and a senior brother of Harbin Engineering. I have been engaged in technology research and development for Tencent and Baidu for many years. I used my spare time to brush leetcode. More exciting algorithm articles are available:   Code Random Thoughts.     After paying attention, reply "Java" "C++" "python" "Resume template" and so on. With the learning materials I have compiled for many years, you can add me to    WeChat , remark "personal profile" + "group review questions", and pull you into the review group (no ads, pure personal sharing), I analyze a classic topic every day. Every topic I choose is not isolated, but from the shallower to the deeper. If you follow the rhythm and read each article continuously, you will definitely be integrated.

Guess you like

Origin blog.csdn.net/youngyangyang04/article/details/108967979