剑指Offer:38 二叉树的深度

一开始想用层序遍历的方法做,写完了层序遍历的函数 (很久没做二叉树了有点生疏),才发现这样求出来的depth是整棵树的节点个数,而不是深度。

    int LevelOrderTraversal(TreeNode* pRoot)
    {
        int depth = 0;
        queue<TreeNode*> Queue;
        Queue.push(pRoot);
        while(!Queue.empty()){
            depth++;
            if(Queue.front()->left != nullptr) Queue.push(Queue.front()->left);
            if(Queue.front()->right != nullptr) Queue.push(Queue.front()->right);
            Queue.pop(Queue.front());
        }
    }

 后来想到用先序遍历的方法,用两个固定的地址存放当前深度和最大深度,不知道为什么一直报错:

运行错误: 请检查是否存在数组、列表等越界非法访问,内存非法访问等情况,case通过率为0.00%

class Solution {
public:
    int TreeDepth(TreeNode* pRoot)
    {
        if(pRoot==nullptr) return -1;
        
        int* pDeepth, *pMaxDeepth;
        *pDeepth = *pMaxDeepth = 0;
        PreTraversal(pRoot, pDeepth, pMaxDeepth);
        return *pMaxDeepth;
    }
    
    void PreTraversal(TreeNode* pRoot, int* pDeepth, int* pMaxDeepth)
    {
        (*pDeepth)++;
        if(*pDeepth>*pMaxDeepth) *pMaxDeepth = *pDeepth;
        if(pRoot->left) PreTraversal(pRoot->left, pDeepth, pMaxDeepth);
        (*pDeepth)--;
        if(pRoot->right) PreTraversal(pRoot->right, pDeepth, pMaxDeepth);
    }
};

不想Debug,直接看了书上的思路:后序遍历,写完之后还是报错: 

段错误: 您的程序发生段错误,可能是数组越界,堆栈溢出(比如,递归调用层数太多)等情况引起,case通过率为0.00%

class Solution {
public:
    int TreeDepth(TreeNode* pRoot) {
        int leftdepth = 0;
        int rightdepth = 0;
        if(pRoot->left!=nullptr) leftdepth = TreeDepth(pRoot->left);
        if(pRoot->right!=nullptr) rightdepth = TreeDepth(pRoot->right);
        return (leftdepth>rightdepth)?(leftdepth+1):(rightdepth+1);
    }
};

最后不知道要怎么改,试了试加了一句判断根节点是不是为0,就可以AC了。

原因应该是:原先的程序,对一开始根节点为空的情况就会出错,其他情况都不会出问题。

class Solution {
public:
    int TreeDepth(TreeNode* pRoot) {
        if(pRoot==nullptr) return 0;
        int leftdepth = 0;
        int rightdepth = 0;
        if(pRoot->left!=nullptr) leftdepth = TreeDepth(pRoot->left);
        if(pRoot->right!=nullptr) rightdepth = TreeDepth(pRoot->right);
        return (leftdepth>rightdepth)?(leftdepth+1):(rightdepth+1);
    }
};

其他解法:

其实层序遍历也可以求出树的深度,思路是:每次遍历下一层之前,将临时变量tmp设置为当前队列的长度,即下一层的节点数。然后下一层每弹出一个节点,tmp减去1,到tmp为0就是下一层也遍历完了,depth++,再为tmp赋值,开始下一层的遍历。

代码:

class Solution {
public:
    int TreeDepth(TreeNode* pRoot) {
        int depth = 0;
        int tmp = 1;
        queue<TreeNode*> Q;
        if(pRoot!=nullptr) Q.push(pRoot); //判断根节点不为空再放入队列,因为空指针放入队列,队列的size还是会+1的
        
        while(Q.size()!=0){
            TreeNode* pNode = Q.front(); Q.pop();
            tmp--;
            if(pNode->left!=nullptr) Q.push(pNode->left);
            if(pNode->right!=nullptr) Q.push(pNode->right);
            if(tmp==0){
                depth++;
                tmp = Q.size();
            }
        }
        return depth;
    }
};

更简洁的递归写法:

class Solution {
public:
    int TreeDepth(TreeNode* pRoot)
    {
        if (pRoot == nullptr) return 0;
        return 1 + max({TreeDepth(pRoot->left), TreeDepth(pRoot->right)});
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值