/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
#define Pair pair<TreeNode*,int>
class Solution {
public:
int maxDepth(TreeNode* root) {
// 递归
// if(root == NULL)
// return 0;
// if(root->left == NULL && root->right == NULL)
// return 1;
// else
// return max(maxDepth(root->left),maxDepth(root->right))+1;
// 迭代
if(root == NULL)
return 0;
stack<Pair> s;
s.push(Pair(root,1));
int depth=0;
while(!s.empty())
{
Pair temp = s.top();
s.pop();
if(temp.first != NULL)
{
depth = depth>temp.second?depth:temp.second;
if(temp.first->left)
s.push(Pair(temp.first->left,temp.second+1));
if(temp.first->right)
s.push(Pair(temp.first->right,temp.second+1));
}
}
return depth;
}
};