代码随想录算法训练营15期 Day 16 | 104.二叉树的最大深度、559.n叉树的最大深度、111.二叉树的最小深度、222.完全二叉树的节点个数

104.二叉树的最大深度

什么是深度,什么是高度,如何求深度,如何求高度,这里有关系到二叉树的遍历方式。

深度是前序遍历,从上往下走;高度是后序遍历,从下往上走,深度为最大高度。

思路:本题的关键是使用高度的方式返回相应的深度,经过几次后序遍历,然后直接返回相应的最大高度就是相应的深度。

题目链接:力扣

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int shendu(TreeNode* root)
    {
        if(root == nullptr)
        return 0;

        int left = shendu(root->left);

        int right = shendu(root->right);

        int height = 1 + max(left,right);

        return height;
    }

    int maxDepth(TreeNode* root) {
        return shendu(root);

    }
};

111.二叉树的最小深度

先看视频讲解,和最大深度 看似差不多,其实 差距还挺大,有坑。含义是根节点的最小高度。

 题目链接:力扣

使用后序遍历的过程,从下往上进行相应的遍历,就是一个左右中遍历的程序,与上面的二叉树方式遍历差不多。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
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);
    }
};

222.完全二叉树的节点个数

需要了解,普通二叉树 怎么求,完全二叉树又怎么求

题目链接:力扣

普通二叉树的节点数量计算代码:

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);
    }
};

完全二叉树的节点

完全二叉树的底层节点从左到右都是存在的,不能够断开,一旦断开就不是完全二叉树了。
相应的遍历过程与之前还是一样的,进行相应的左右递归过程

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:


    int countNodes(TreeNode* root) {

        if(root == nullptr) return 0;

        TreeNode* left = root->left;

        TreeNode* right = root->right;

        int cleft = 0,cright = 0;

        while(left)
        {
            left = left->left;
            cleft++;
        }
        while(right)
        {
            right = right->right;
            cright++;
        }

         if (cleft == cright) {
            return (2 << cleft) - 1; // 注意(2<<1) 相当于2^2,所以leftDepth初始为0
        }

       return countNodes(root->left) + countNodes(root->right) + 1;
    }
};

猜你喜欢

转载自blog.csdn.net/m0_47489229/article/details/131117058