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)
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 相关阅读:
    使用metasploit进行栈溢出攻击-2
    使用metasploit进行栈溢出攻击-1
    如何利用 Visual Studio 自带工具提高开发效率
    Visaul Studio 常用快捷键动画演示
    WinForm AutoComplete 输入提示、自动补全
    RTL2832U+R820T电视棒windows下安装sdr# 以及搭建ADS-B使用VirtualRadar看飞机的教程
    传说中的WCF(12):服务器回调有啥用
    传说中的WCF(11):会话(Session)
    传说中的WCF(10):消息拦截与篡改
    传说中的WCF(9):流与文件传输
  • 原文地址:https://www.cnblogs.com/BoysCryToo/p/13392465.html
Copyright © 2011-2022 走看看