1 104 . 二叉树的最大深度
知识点:
- 二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始)
- 二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数或者节点数(取决于高度从0开始还是从1开始)
而根节点的高度就是二叉树的最大深度
ac迭代版本(层序遍历):
/**
* 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)
{
// 层序遍历
queue<TreeNode*> q;
if(root != nullptr)
{
q.push(root);
}
else 0;
int ans = 0;
while(!q.empty())
{
int size = q.size();;
for(int i = 0; i <size; i++)
{
TreeNode* front = q.front();
q.pop();
if(front->left != nullptr)q.push(front->left);
if(front->right != nullptr)q.push(front->right);
}
ans++;
}
return ans;
}
};
ac递归版本:carl思路是叶子往上数, 我的思路是根节点往下数,如下。
/**
* 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:
void dfs(TreeNode* root,int depth,int& maxDepth) // 本root在整个树中是第几层
{
if(root == nullptr)return;
maxDepth = max(maxDepth,depth);
dfs(root->left,depth+1,maxDepth);
dfs(root->right,depth+1,maxDepth);
}
int maxDepth(TreeNode* root)
{
int maxDepth = 1;
if(root)
{
dfs(root,1,maxDepth);
return maxDepth;
}
else return 0;
}
};
2 111. 二叉树的最小深度
我用了迭代(层序遍历),迭代解法略。AC代码:
/**
* 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) {
int ans = 0;
queue<TreeNode*> q;
if(root != nullptr)
{
q.push(root);
}
else return ans;
ans = INT_MAX;
int tmp = 1;
while(!q.empty())
{
int size = q.size();;
for(int i = 0; i <size; i++)
{
TreeNode* front = q.front();
q.pop();
if(front->left != nullptr)q.push(front->left);
if(front->right != nullptr)q.push(front->right);
if(front->left == nullptr && front->right == nullptr)
{
ans = min(tmp,ans);
}
}
tmp++;
}
return ans;
}
};
3 222. 完全二叉树的节点个数
当成普通二叉树的做法:
AC代码:
/**
* 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) {
queue<TreeNode*> que;
if(!root)return 0;
else que.push(root);
int ans = 0;
while(!que.empty())
{
int size = que.size();
for(int i = 0; i < size; i++)
{
TreeNode* tmp = que.front();
ans++;
que.pop();
if(tmp->left)que.push(tmp->left);
if(tmp->right)que.push(tmp->right);
}
}
return ans;
}
};
利用完全二叉树的性质:
感觉有点麻烦,先跳过。link