day16 - 二叉树part03

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 maxDepth(TreeNode* root) {
        int result = 0;
        queue<TreeNode*> queue_1;
        if(root != NULL) queue_1.push(root);
        while(!empty(queue_1)){
            int size = queue_1.size();
            for(int i=0; i<size; i++){
                TreeNode* node = queue_1.front();
                queue_1.pop();
                if(node->left) queue_1.push(node->left);
                if(node->right) queue_1.push(node->right);
            }
            result++;
        }
        return result;
    }
    */

    //递归
    int search(TreeNode* root, int depth){
        if(root == NULL) return depth;
        int depth_left = search(root->left, depth + 1);
        int depth_right = search(root->right, depth + 1);
        return max(depth_left, depth_right);
    }

    int maxDepth(TreeNode* root) {
        return search(root, 0);
    }
};

 559. N 叉树的最大深度

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/

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

 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 minDepth(TreeNode* root) {
        if (root == NULL) return 0;
        int depth = 0;
        queue<TreeNode*> que;
        que.push(root);
        while(!que.empty()) {
            int size = que.size();
            depth++; // 记录最小深度
            for (int i = 0; i < size; i++) {
                TreeNode* node = que.front();
                que.pop();
                if (node->left) que.push(node->left);
                if (node->right) que.push(node->right);
                if (!node->left && !node->right) { // 当左右孩子都为空的时候,说明是最低点的一层了,退出
                    return depth;
                }
            }
        }
        return depth;
    }
    */

    //递归
    int search(TreeNode* node){
        if(node == NULL) return 0;
        int left_depth = search(node->left);
        int right_depth = search(node->right);
        // 当一个左子树为空,右不为空,这时并不是最低点
        if (node->left == NULL && node->right != NULL) { 
            return 1 + right_depth;
        }   
        // 当一个右子树为空,左不为空,这时并不是最低点
        if (node->left != NULL && node->right == NULL) { 
            return 1 + left_depth;
        }
        return min(left_depth, right_depth) + 1;

    }

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

};

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

/**
 * 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 == NULL) return 0;
        return countNodes(root->left) + countNodes(root->right) + 1;
        */

        if (root == nullptr) return 0;
        TreeNode* left = root->left;
        TreeNode* right = root->right;
        int leftDepth = 0, rightDepth = 0; // 这里初始为0是有目的的,为了下面求指数方便
        while (left) {  // 求左子树深度
            left = left->left;
            leftDepth++;
        }
        while (right) { // 求右子树深度
            right = right->right;
            rightDepth++;
        }
        if (leftDepth == rightDepth) {
            return (2 << leftDepth) - 1; // 注意(2<<1) 相当于2^2,所以leftDepth初始为0
        }
        return countNodes(root->left) + countNodes(root->right) + 1;
    }
};

 

posted @ 2023-08-24 14:54  zqh2023  阅读(263)  评论(0)    收藏  举报