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;
}
};
今毕。