71.二叉树的深度

 

 

https://www.cnblogs.com/king-lps/p/10748535.html

http://39.96.217.32/blog/4#comment-container

https://www.cnblogs.com/du001011/p/11229170.html

class Solution {
public:
    int treeDepth(TreeNode* root) {
        //1.终止条件:当树为空时结束递归,并返回当前深度0
        if(root == NULL){
            return 0;
        }
        //3.本级递归应该做什么.root的左、右子树的最大深度
        int leftDepth = treeDepth(root->left);
        int rightDepth = treeDepth(root->right);
        //2.找返回值。返回的是左右子树的最大深度+1
        return max(leftDepth, rightDepth) + 1;
        
    }
};

 

注意,表示节点为空

正确:if(root == NULL)
正确:if(!root )
错误:if(root == null)

 

posted @ 2020-02-20 22:11  靖愁  阅读(141)  评论(0编辑  收藏  举报