最大深度
1)非递归遍历
以层次遍历为例,对层次遍历稍加改写,遍历过程记录层数即可,遍历所有结点返回depth(注意与最小深度区分)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode *root){
if(!root) return 0;
queue<TreeNode *> q;
TreeNode *now;
q.push(root);
int depth=0;
while(!q.empty()){
depth++;
int cnt=0;
int qlength=q.size();
while(cnt<qlength){
cnt++;
now=q.front();
q.pop();
if(now->left) q.push(now->left);
if(now->right) q.push(now->right);
}
}
return depth;
}
};
2)递归遍历:
递归进入到二叉树的最后一层的左右子树(空),然后逐层返回,每return一下,高度+1
int maxDepth2(TreeNode *root){
if(!root) return 0;
return max(maxDepth2(root->lchild),maxDepth2(root->rchild))+1;
}
最小深度
1)非递归遍历:
助队列进行层次遍历,累加层数,若某一层的某个结点没有左右子树则返回该结点所在层数,over(没有遍历完所有结点)(题目出自leetcode)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode* root) {
if(!root) return 0;
queue<TreeNode *> q;
TreeNode *now;
q.push(root);
int depth=0;
while(!q.empty()){
depth++;
int cnt=0;
int qlength=q.size();
while(cnt<qlength){
cnt++;
now=q.front();
q.pop();
if(now->left) q.push(now->left);
if(now->right) q.push(now->right);
if(!now->left && !now->right) return depth;
}
}
}
};
递归写法:
1)空树返回0,只有一个根节点返回1
2)若根节点的左子树为空,对右子树进行算法递问
若根节点的右子树为空,对左子树进行算法递问 (可能出现一边倾斜的那种问)
3)返回左右子树的最小高度+1
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode* root) {
if(!root) return 0;
if(!root->left && !root->right) return 1;
if(!root->left)
return minDepth( root->right)+1;
if(!root->right)
return minDepth( root->left)+1;
return min(minDepth( root->left),minDepth( root->right))+1;
}
};
完整代码
#include<iostream>
#include<queue>
#include<stack>
using namespace std;
struct TreeNode{
int val;
TreeNode *lchild,*rchild;
};
//递归实现
int maxDepth1(TreeNode *root){
if(!root) return 0;
return max(maxDepth1(root->lchild),maxDepth1(root->rchild))+1;
}
//最大深度 递归实现
int maxDepth2(TreeNode *root){
if(!root) return 0;
queue<TreeNode *> q;
TreeNode *now;
q.push(root);
int depth=0;
while(!q.empty()){
depth++;
int cnt=0;
int qlength=q.size();
while(cnt<qlength){
cnt++;
now=q.front();
q.pop();
if(now->lchild) q.push(now->lchild);
if(now->rchild) q.push(now->rchild);
}
}
return depth;
}
//递归实现
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
int minDepth1(TreeNode* root) {
if(!root) return 0;
if(!root->lchild&& !root->rchild) return 1;
if(!root->lchild)
return minDepth1( root->rchild)+1;
if(!root->rchild)
return minDepth1( root->lchild)+1;
return min(minDepth1( root->lchild),minDepth1( root->rchild))+1;
}
//非递归实现
int minDepth2(TreeNode *root){
if(!root) return 0;
queue<TreeNode *> q;
TreeNode *now;
q.push(root);
int depth=0;
while(!q.empty()){
depth++;
int cnt=0;
int qlength=q.size();
while(cnt<qlength){
cnt++;
now=q.front();
q.pop();
if(now->lchild) q.push(now->lchild);
if(now->rchild) q.push(now->rchild);
if(!now->lchild && !now->rchild) return depth;
}
}
return depth;
}
int main(){
TreeNode *root=new TreeNode;
root->val=1;
root->lchild=new TreeNode;
root->rchild=new TreeNode;
root->lchild->val=2;
root->rchild->val=3;
root->lchild->lchild=new TreeNode;
root->lchild->rchild=new TreeNode;
root->lchild->lchild->val=4;
root->lchild->rchild->val=5;
root->lchild->lchild->lchild=NULL;
root->lchild->lchild->rchild=NULL;
root->lchild->rchild->lchild=NULL;
root->lchild->rchild->rchild=NULL;
root->rchild->lchild=NULL;
root->rchild->rchild=NULL;
int max_depth1=maxDepth1(root);
int max_depth2=maxDepth2(root);
cout<<"max_depth of binary tree:\n"<<"\n递归遍历:"<<max_depth1<<endl<<"\n非递归遍历:"<<max_depth2<<endl;
int min_depth1=minDepth1(root);
int min_depth2=minDepth2(root);
cout<<"min_depth of binary tree:\n"<<"\n递归遍历:"<<min_depth1<<endl<<"\n非递归遍历:"<<min_depth2<<endl;
}
测试截图: