代码随想录刷题第16天

第一题是二叉树的最大深度https://leetcode.cn/problems/maximum-depth-of-binary-tree/description/,先看的视频讲解代码随想录。区分了高度与深度,一个指向到叶子结点的距离,一个指向到根节点的距离。因此求最大深度的过程就可以转换为求根节点的最大高度。重点在于后序遍历的应用,递归代码如下:

class Solution {
public:
    int getheight(TreeNode* node){
        if (node == NULL) return 0;
        int leftheight = getheight(node->left);
        int rightheight = getheight(node->right);
        int height = max(leftheight,rightheight) + 1;
        return height;
    }
    int maxDepth(TreeNode* root) {
    return getheight(root);
    }
};

前序遍历代码如下:

class Solution {
public:
    int result;
    void getdepth(TreeNode* node, int depth) {
        result = depth > result ? depth : result;
        if (node->left == NULL && node->right == NULL)
            return;
        if (node->left) {
            depth++;
            getdepth(node->left, depth);
            depth--;
        }
        if (node->right) {
            depth++;
            getdepth(node->right, depth);
            depth--;
        }
        return;
    }
    int maxDepth(TreeNode* root) {
        result = 0;
        if (root == NULL)
            return result;
        getdepth(root, 1);
        return result;
    }
};

求N叉树最大深度的递归代码如下:

class Solution {
public:
    int getDepth(Node* root) {
        if (root == NULL)
            return 0;
        int depth = 0;
        for (int i = 0; i < root->children.size(); i++) {
            depth = max(depth, getDepth(root->children[i]));
        }
        return depth + 1;
    }
    int maxDepth(Node* root) { return getDepth(root); }
};

迭代代码如下:

class Solution {
public:
    int maxDepth(Node* root) {
        queue que;
        if (root != NULL)
            que.push(root);
        int depth = 0;
        while (!que.empty()) {
            int size = que.size();
            depth++;
            while (size--) {
                Node* node = que.front();
                que.pop();
                for (int i = 0; i < node->children.size(); i++) {
                    if (node->children[i])
                        que.push(node->children[i]);
                }
            }
        }
        return depth;
    }
};

第二题是求二叉树的最小深度https://leetcode.cn/problems/minimum-depth-of-binary-tree/description/,重点也是后序遍历的过程,通过求最小高度转化为最小深度。

class Solution {
public:
    int getDepth(TreeNode* node) {
        if (node == NULL)
            return 0;
        int depth = 1;
        int leftDepth = getDepth(node->left);
        int rightDepth = getDepth(node->right);
        if (leftDepth == NULL && rightDepth != NULL)
            depth = rightDepth + 1;
        if (leftDepth != NULL && rightDepth == NULL)
            depth = leftDepth + 1;
        if (leftDepth != NULL && rightDepth != NULL)
            depth = min(leftDepth, rightDepth) + 1;
        return depth;
    }
    int minDepth(TreeNode* root) { return getDepth(root); }
};

前序遍历就是直接求最小深度。

class Solution {
private:
    int result;
    void getdepth(TreeNode* node, int depth) {
        if (node == nullptr)
            return;
        if (node->left == nullptr && node->right == nullptr) {
            result = min(result, depth);
        }
        if (node->left)
            getdepth(node->left, depth + 1);
        if (node->right)
            getdepth(node->right, depth + 1);
        return;
    }

public:
    int minDepth(TreeNode* root) {
        if (root == nullptr)
            return 0;
        result = INT_MAX;
        getdepth(root, 1);
        return result;
    }
};

第三题是二叉树的节点个数https://leetcode.cn/problems/count-complete-tree-nodes/description/,先想到的是层序遍历,试一下。成功拿下!

class Solution {
public:
    int countNodes(TreeNode* root) {
        queue que;
        if (root != NULL)
            que.push(root);
        int result = 0;
        while (!que.empty()) {
            int size = que.size();
            result += size;
            while (size--) {
                TreeNode* node = que.front();
                que.pop();
                if (node->left)
                    que.push(node->left);
                if (node->right)
                    que.push(node->right);
            }
        }
        return result;
    }
};

看看卡哥怎么说代码随想录,卡哥先提到了求一般二叉树的思路,前中后序遍历均可。

class Solution {
public://后序遍历
    int nodenumber(TreeNode* node) {
        if (node == NULL)
            return 0;
        int leftnum = nodenumber(node->left);
        int rightnum = nodenumber(node->right);
        return leftnum + rightnum + 1;
    }
    int countNodes(TreeNode* root) { return nodenumber(root); }
};

完全二叉树的思路是通过递归找到子树中的满二叉树,利用公式求出满二叉树的节点个数并相加得到结果,很精巧的思路。

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

总体来看今天的题目是递归的进一步运用。

你可能感兴趣的:(数据结构,leetcode,算法)