二叉樹的最大深度與最小深度
阿新 • • 發佈:2018-11-11
最大深度
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;
}
測試截圖: