Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode *root)
{
int deep=0;
if(root)
{
int lchilddeep=maxDepth(root->left);
int rchilddeep=maxDepth(root->right);
deep=lchilddeep>=rchilddeep?lchilddeep+1:rchilddeep+1;
}
return deep;
}
};
int TreeDeep(BinTree BT ){
int treedeep=0;
stack S;
stack tag;
BinTree p=BT;
while(p!=NULL||!isEmpty(S)){
while(p!=NULL){
push(S,p);
push(tag,0);
p=p->lchild;
}
if(Top(tag)==1){
deeptree=deeptree>S.length?deeptree:S.length;
pop(S);
pop(tag);
p=NULL;
}else{
p=Top(S);
p=p->rchild;
pop(tag);
push(tag,1);
}
}
return deeptree;
}