104.二叉树的最大深度
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 maxDepth(TreeNode* root) {
queue<TreeNode*> que;
if(root!=NULL) que.push(root);
int deepth=0;
while(!que.empty()){
int size=que.size();
for(int i=0;i<size;i++){
TreeNode* cur=que.front();
que.pop();
if(cur->left) que.push(cur->left);
if(cur->right) que.push(cur->right);
}
deepth++;//每遍历一层深度+1
}
return deepth;
}
};
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 maxDepth(TreeNode* root) {
if(root==NULL) return 0;
return 1+max(maxDepth(root->left),maxDepth(root->right));
}
};
3.递归的完整写法(以前序遍历为例)
/**
* 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 result;
void getDepth(TreeNode* node,int deep){
result= result>deep? result:deep;//中
if(node->left==NULL&&node->right==NULL) return;
if(node->left) getDepth(node->left,deep+1);//左
if(node->right) getDepth(node->right,deep+1);//右
return;
}
int maxDepth(TreeNode* root) {
result=0;
if(root==0) return result;
getDepth(root,1); return result;
}
};
1.递归法
/*
// 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 deepth=0;
for(int i=0;i<root->children.size();i++){//求出N个子树的最大深度
deepth=max(deepth,maxDepth(root->children[i]));
}
return deepth+1;//子树深度+1才是总深度
}
};
2.迭代法
/*
// 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) {
queue<Node*> que;
if(root==NULL) return 0;
que.push(root);
int deepth =0;
while(!que.empty()){
deepth++;
int size=que.size();
for(int i=0;i<size;i++){
Node* cur=que.front();
que.pop();
for(int j=0;j<cur->children.size();j++){
if(cur->children[j])
que.push(cur->children[j]);
}
}
}
return deepth;
}
};
111.二叉树的最小深度
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 minDepth(TreeNode* root) {//层序遍历,找到第一个左右孩子都为空的结点并返回深度
if(root==NULL) return 0;
queue<TreeNode*> que;
if(root!=NULL) que.push(root);
int deepth=0;
while(!que.empty()){
deepth++;
int size=que.size();
for(int i=0;i<size;i++){
TreeNode* cur=que.front();
que.pop();
if(!cur->left&&!cur->right) return deepth;//左右孩子都为空,到达最低层
if(cur->left) que.push(cur->left);
if(cur->right) que.push(cur->right);
}
}
return deepth;
}
};
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 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;
int result=1+min(leftDepth,rightDepth);
return result;
}
int minDepth(TreeNode* root) {
return getDepth(root);
}
};
3.递归法(前序遍历版)
/**
* 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 result;//全局变量
void getDepth(TreeNode* root,int deep){
if(root==NULL) return;//递归终止条件
if(!root->left&&!root->right)//中
result= min(result,deep);
if(root->left)
getDepth(root->left,deep+1);//左
if(root->right)//右
getDepth(root->right,deep+1);
return;
}
int minDepth(TreeNode* root) {
if(root==NULL) return 0;
result=INT_MAX;//设置一个最大值,方便交换
getDepth(root,1);
return result;
}
};
222.完全二叉树的节点个数
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 countNodes(TreeNode* root) {
stack<TreeNode*> st;
int result=0;
if (root == NULL) return result;
st.push(root);
while (!st.empty()) {
TreeNode* node = st.top(); // 中
st.pop();
result++;
if (node->right) st.push(node->right); // 右(空节点不入栈)
if (node->left) st.push(node->left); // 左(空节点不入栈)
}
return result;
}
};
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 countNodes(TreeNode* root) {
queue<TreeNode*> que;
if (root != NULL) que.push(root);
int result=0;
while (!que.empty()) {
int size = que.size();
// 这里一定要使用固定大小size,不要使用que.size(),因为que.size是不断变化的
for (int i = 0; i < size; i++) {
TreeNode* node = que.front();
que.pop();
result++;
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
}
return result;
}
};
3.递归法(代码非常简洁)
/**
* 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) return 0;
return 1+countNodes(root->left)+countNodes(root->right);
}
};
今日总结:好好好。