代码随想录算法训练营第十四天打卡

104.二叉树的最大深度

代码如下:

class Solution {
public:
    int maxDepth(TreeNode* root) {
        queue<TreeNode*> q;
        int depth = 0;
        if (root == nullptr) {
            return depth;
        }
        q.push(root);
        while (!q.empty()) {
            int size = q.size();
            if (size) {
                depth++;
            }
            for (int i = 0; i < size; i++) {
                TreeNode* curr = q.front();
                q.pop();
                if (curr->left) {
                    q.push(curr->left);
                }
                if (curr->right) {
                    q.push(curr->right);
                }
            }
        }
        return depth;
    }
};

完整代码如下:

#include <iostream>
#include <queue>

using namespace std;

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

class Solution {
public:
    int maxDepth(TreeNode* root) {
        queue<TreeNode*> q;
        int depth = 0;
        if (root == nullptr) {
            return depth;
        }
        q.push(root);
        while (!q.empty()) {
            int size = q.size();
            if (size) {
                depth++;
            }
            for (int i = 0; i < size; i++) {
                TreeNode* curr = q.front();
                q.pop();
                if (curr->left) {
                    q.push(curr->left);
                }
                if (curr->right) {
                    q.push(curr->right);
                }
            }
        }
        return depth;
    }
};

int main() {
    Solution sol;
    TreeNode* root = new TreeNode(3);
    root->left = new TreeNode(9);
    root->right = new TreeNode(20);
    root->right->left = new TreeNode(15);
    root->right->right = new TreeNode(7);
    int ans = sol.maxDepth(root);
    cout << ans << endl;
}

559.n叉树的最大深度

代码如下:

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

完整代码如下:

#include <iostream>
#include <vector>
#include <queue>

using namespace std;

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) {
        int ans = 0;
        if (root == nullptr) {
            return ans;
        }
        queue<Node*> q;
        q.push(root);
        while (!q.empty()) {
            ans++;
            int size = q.size();
            for (int i = 0; i < size; i++) {
                Node* curr = q.front();
                q.pop();
                if (curr->children.size()) {
                    for (int j = 0; j < curr->children.size(); j++) {
                        q.push(curr->children[j]);
                    }
                }
            }
        }
        return ans;
    }
};

int main() {
    return 0;
}

111.二叉树的最小深度

代码如下:

class Solution {
public:
    int minDepth(TreeNode* root) {
        queue<TreeNode*> q;
        int depth = 0;
        if (root == nullptr) {
            return depth;
        }
        q.push(root);
        while (!q.empty()) {
            int size = q.size();
            depth++;
            for (int i = 0; i < size; i++) {
                TreeNode* curr = q.front();
                q.pop();
                if (curr->left) {
                    q.push(curr->left);
                }
                if (curr->right) {
                    q.push(curr->right);
                }
                if (curr->left == nullptr && curr->right == nullptr) {
                    return depth;
                }
            }
        }
        return depth;
    }
};

完整代码如下:

#include <iostream>
#include <queue>

using namespace std;

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
};

class Solution {
public:
    int minDepth(TreeNode* root) {
        queue<TreeNode*> q;
        int depth = 0;
        if (root == nullptr) {
            return depth;
        }
        q.push(root);
        while (!q.empty()) {
            int size = q.size();
            depth++;
            for (int i = 0; i < size; i++) {
                TreeNode* curr = q.front();
                q.pop();
                if (curr->left) {
                    q.push(curr->left);
                }
                if (curr->right) {
                    q.push(curr->right);
                }
                if (curr->left == nullptr && curr->right == nullptr) {
                    return depth;
                }
            }
        }
        return depth;
    }
};

int main() {
    Solution sol;
    TreeNode* root = new TreeNode(3);
    root->left = new TreeNode(9);
    root->right = new TreeNode(20);
    root->right->left = new TreeNode(15);
    root->right->right = new TreeNode(7);
    int ans = sol.minDepth(root);
    cout << ans << endl;
}

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

递归法代码如下:

class Solution {
public:
    int countNodes(TreeNode* root) {
        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;
    }
};

迭代法代码如下:

class Solution {
public:
    int countNodes(TreeNode* root) { //方法一:层序遍历法
        int count = 0;
        if (root == nullptr) {
            return count;
        }
        queue<TreeNode*> q;
        q.push(root); 
        while (!q.empty()) {
            int size = q.size();
            count += size;
            for (int i = 0; i < size; i++) {
                TreeNode* curr = q.front();
                q.pop();
                if (curr->left) {
                    q.push(curr->left);
                }
                if (curr->right) {
                    q.push(curr->right);
                }
            }
        }
        return count;
    }
};

今毕。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值