题目描述
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
class Solution {
public:
/**
*
* @param root TreeNode类
* @return int整型
*/
int run(TreeNode* root) {
// write code here
if (root==nullptr) return 0;
if (root->left == nullptr)
{
return run(root->right) +1;
}
if (root->right == nullptr)
{
return run(root->left)+1;
}
int leftDepth=run(root->left);
int rightDepth=run(root->right);
return (leftDepth <rightDepth) ?(leftDepth+1):(rightDepth+1);
}
};
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
class Solution {
public:
typedef TreeNode *tree;
/**
*
* @param root TreeNode类
* @return int整型
*/
int run(TreeNode* root) {
// write code here
if (!root) return 0;
queue <tree> qu;
tree last,now;
int level,size;
last=now=root;
level=1;qu.push(root);
while (qu.size()){
now=qu.front();
qu.pop();
size=qu.size();
if (now->left) qu.push(now->left);
if (now->right) qu.push(now->right);
if (qu.size()-size==0) break;
if (last==now){
level++;
if (qu.size()) last=qu.back();
}
}
return level;
}
};