zoukankan      html  css  js  c++  java
  • 104. 二叉树的最大深度

    题目

    104. 二叉树的最大深度

    我的思路

    深搜递归,广搜队列

    我的实现

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        int maxDepth(TreeNode* root) {
            //广搜三件套:初始化队列,加一个队头,遍历队列
            //队列元素:节点+当前层数     
            int maxDepth=0;
            queue<pair<TreeNode*,int>> q;
            if(root!=NULL)
            q.push(make_pair(root,1));
            while(!q.empty()){
                pair temp = q.front();
                q.pop();
                maxDepth = max(maxDepth,temp.second);
                if(temp.first->left!=NULL) q.push(make_pair(temp.first->left,temp.second+1));
                if(temp.first->right!=NULL) q.push(make_pair(temp.first->right,temp.second+1));
            }
            return maxDepth;
        }
    };
    //深搜or广搜
    /*深搜
    class Solution {
    public:
        int search(TreeNode *root){
            if(root==NULL)return 0;
            int length = 1;
            if (root->left!=NULL){
                length = search(root->left)+1;
            }
            if(root->right!=NULL){
                length = max(length,search(root->right)+1);
            }
            return length;
        }
        int maxDepth(TreeNode* root) {
            return search(root);
        }
    };
    */
    /*广搜
    class Solution {
    public:
        int maxDepth(TreeNode* root) {
            //广搜三件套:初始化队列,加一个队头,遍历队列
            //队列元素:节点+当前层数     
            int maxDepth=0;
            queue<pair<TreeNode*,int>> q;
            if(root!=NULL)
            q.push(make_pair(root,1));
            while(!q.empty()){
                pair temp = q.front();
                q.pop();
                maxDepth = max(maxDepth,temp.second);
                if(temp.first->left!=NULL) q.push(make_pair(temp.first->left,temp.second+1));
                if(temp.first->right!=NULL) q.push(make_pair(temp.first->right,temp.second+1));
            }
            return maxDepth;
        }
    };
    */

    深搜广搜时间复杂度都是n,n为节点数。空间复杂度都是logn,层数。

    拓展学习

    更简洁的深搜写法

    class Solution {
    public:
        int maxDepth(TreeNode* root) {
            if (root == nullptr) return 0;
            return max(maxDepth(root->left), maxDepth(root->right)) + 1;
        }
    };
    
    作者:LeetCode-Solution
    链接:https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/solution/er-cha-shu-de-zui-da-shen-du-by-leetcode-solution/
    来源:力扣(LeetCode)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    (转+原)python中的浅拷贝和深拷贝
    (原)torch7中添加新的层
    (原+转)ubuntu终端输出彩色文字
    (原)torch中显示nn.Sequential()网络的详细情况
    (原)python中使用plt.show()时显示图像
    eclipse 注释模板
    leetcode 11 最大盛水容器
    leetcode 9 回文数字
    leetcode8 字符串转整数
    利用Intent启动activity的例子
  • 原文地址:https://www.cnblogs.com/BoysCryToo/p/13392465.html
Copyright © 2011-2022 走看看