104.二叉树的最大深度
1.层序遍历
每层遍历完++
/**
* 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 depth=0;
if(root==NULL) return depth;
TreeNode* cur = root;
queue<TreeNode*> que;
que.push(cur);
while(!que.empty()){
int size = que.size();
for(int i=0; i<size; i++){
cur = que.front();
que.pop();
if(cur->left) que.push(cur->left);
if(cur->right) que.push(cur->right);
}
depth++;
}
return depth;
}
};
2.递归
单层递归的逻辑:先求它的左子树的深度,再求右子树的深度,最后取左右深度最大的数值 再+1 (加1是因为算上当前中间节点)就是目前节点为根节点的树的深度。
/**
* 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 getdepth(TreeNode* root){
if(root==NULL) return 0;
int leftdepth = getdepth(root->left);
int rightdepth = getdepth(root->right);
return 1+max(leftdepth, rightdepth);
}
int maxDepth(TreeNode* root) {
return getdepth(root);
}
};
111.二叉树的最小深度
1.递归
最小深度是从根节点到最近叶子节点的最短路径上的节点数量
左右孩子都为空的节点才是叶子节点,如果左子树为空,右子树不为空,说明最小深度是 1 + 右子树的深度。反之,右子树为空,左子树不为空,最小深度是 1 + 左子树的深度。 最后如果左右子树都不为空,返回左右子树深度最小值 + 1 。
/**
* 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 getdepth(TreeNode* root){
if(root==NULL) return 0;
int leftdepth = getdepth(root->left);
int rightdepth = getdepth(root->right);
if(root->left==NULL && root->right!=NULL){
return 1+rightdepth;
}
if(root->left!=NULL && root->right==NULL){
return 1+leftdepth;
}
return 1+min(rightdepth, leftdepth);
}
int minDepth(TreeNode* root) {
return getdepth(root);
}
};
2.迭代-层序遍历
/**
* 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) return 0;
queue<TreeNode*> que;
que.push(root);
int count = 0;
while(!que.empty()){
int size = que.size();
count++;
for(int i=0;i<size;i++){
TreeNode* cur = que.front();
que.pop();
if(!cur->left&&!cur->right)return count;
if(cur->left)que.push(cur->left);
if(cur->right)que.push(cur->right);
}
}
return count;
}
};
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;
TreeNode* left = root->left;
TreeNode* right = root->right;
int leftsum = 0;
int rightsum = 0;
while(left){
left = left->left;
leftsum++;
}
while(right){
right = right->right;
rightsum++;
}
if(leftsum==rightsum) return (2<<leftsum)-1;
return countNodes(root->left) + countNodes(root->right) + 1;
}
};