/**
* 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;
//无左节点时,对于此时根节点来说,左边为0(舍弃),只考虑右边
else if(root->left==nullptr){
return run(root->right)+1;
}
//同上
else if(root->right==nullptr){
return run(root->left)+1;
}
//左右结点都不为空
else {
//左边最小等于左子树最小
int lefthigh=run(root->left);
//右边最小等于右子树最小
int righthigh=run(root->right);
//比较左右,得到层数最小叶节点
if(lefthigh>righthigh) return righthigh+1;
else{
return lefthigh+1;
}
}
}
};