[Leetcode 60 days with brush] day16 binary tree - 104. The maximum depth of the binary tree, 111. The minimum depth of the binary tree, 222. The number of nodes in the complete binary tree


  topic:

104. Maximum depth of binary tree

Given a binary tree, find its maximum depth.

The depth of a binary tree is the number of nodes on the longest path from the root node to the furthest leaf node.

Explanation:  A leaf node refers to a node without child nodes.

Example:
Given a binary tree  [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

Return its maximum depth of 3 .


Thinking process and knowledge points:

For this question, you can use the preorder (middle, left, and right), or the postorder traversal (left, right, middle). The depth is obtained by using the preorder, and the height is obtained by using the postorder.

  • The depth of a binary tree node: refers to the number of edges or nodes of the longest simple path from the root node to the node (depending on whether the depth starts from 0 or 1)
  • The height of a binary tree node: refers to the number of edges or nodes of the longest simple path from the node to the leaf node (depending on whether the height starts from 0 or 1)

The height of the root node is the maximum depth of the binary tree , so in this question we obtain the maximum depth of the binary tree through the height of the root node obtained in the postorder.

I first use postorder traversal (left and right middle) to calculate the height of the tree. First find the depth of its left subtree, then find the depth of the right subtree, and finally take the value with the largest left and right depth and then +1 (adding 1 because the current intermediate node is counted) is the depth of the tree whose current node is the root node.


 answer:

class solution {
public:
    int getdepth(TreeNode* node) {
        if (node == NULL) return 0;
        int leftdepth = getdepth(node->left);       // 左
        int rightdepth = getdepth(node->right);     // 右
        int depth = 1 + max(leftdepth, rightdepth); // 中
        return depth;
    }
    int maxDepth(TreeNode* root) {
        return getdepth(root);
    }
};

topic:

111. Minimum depth of binary tree

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes on the shortest path from the root node to the nearest leaf node.

Note: A leaf node refers to a node that has no child nodes.

Example 1:

Input: root = [3,9,20,null,null,15,7]
 Output: 2

Example 2:

Input: root = [2,null,3,null,4,null,5,null,6]
 Output: 5

hint:

  • The range of the number of nodes in the tree [0, 105] is within
  • -1000 <= Node.val <= 1000

思考过程与知识点:

This question is still both pre-order traversal and post-order traversal. The pre-order seeks the depth, and the post-order seeks the height.

  • The depth of a binary tree node: refers to the number of edges or nodes of the longest simple path from the root node to the node (depending on whether the depth starts from 0 or 1)
  • The height of a binary tree node: refers to the number of longest simple path edges from the node to the leaf node and the number of nodes (depending on whether the height starts from 0 or 1)

Then using post-order traversal, in fact, the minimum distance from the root node to the leaf node is obtained, which is the process of obtaining the height, but this minimum distance is also the minimum depth.


 answer:

class Solution {
public:
    int getDepth(TreeNode* node) {
        if (node == NULL) return 0;
        int leftDepth = getDepth(node->left);           // 左
        int rightDepth = getDepth(node->right);         // 右
                                                        // 中
        // 当一个左子树为空,右不为空,这时并不是最低点
        if (node->left == NULL && node->right != NULL) { 
            return 1 + rightDepth;
        }   
        // 当一个右子树为空,左不为空,这时并不是最低点
        if (node->left != NULL && node->right == NULL) { 
            return 1 + leftDepth;
        }
        int result = 1 + min(leftDepth, rightDepth);
        return result;
    }

    int minDepth(TreeNode* root) {
        return getDepth(root);
    }
};

topic:

222. The number of nodes in a complete binary tree

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

The definition of a complete binary tree  is as follows: In a complete binary tree, except for the bottom node that may not be filled, the number of nodes in each layer reaches the maximum value, and the nodes in the bottom layer are all concentrated in the leftmost positions of the layer. If the lowest level is  h level, then the level contains  1~ 2h nodes.

Example 1:

Input: root = [1,2,3,4,5,6]
 Output: 6

Example 2:

Input: root = []
 Output: 0

Example 3:

Input: root = [1]
 Output: 1

hint:

  • The number of nodes in the tree ranges from[0, 5 * 104]
  • 0 <= Node.val <= 5 * 104
  • The title data ensures that the input tree is  a complete binary tree

Thinking process and knowledge points:

First, find it according to the logic of ordinary binary tree.

The recursive method of this question is similar to the writing method of finding the depth of a binary tree, while the iterative method, binary tree: layer order traversal debut! (opens new window) Slightly modify the traversal template to record the number of nodes traversed.

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


 answer:

class Solution {
private:
    int getNodesNum(TreeNode* cur) {
        if (cur == NULL) 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);
    }
};


Welcome to like, bookmark, comment, your encouragement is the biggest motivation for my creation! (๑╹◡╹)ノ"""

Copyright statement: This article is an original article of CSDN blogger "Dumengjiu", which follows the CC 4.0 BY-SA copyright agreement. For reprinting, please attach the original source link and this statement.
Original link: Dumengjiu's blog_CSDN blog-csdn domain blogger

Guess you like

Origin blog.csdn.net/weixin_53310927/article/details/131286118