题解 | #二叉树的最大宽度#

二叉树的最大宽度

https://www.nowcoder.com/practice/0975d62a307549cea32f353f354a7377

/**
 * struct TreeNode {
 *  int val;
 *  struct TreeNode *left;
 *  struct TreeNode *right;
 *  TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 * };
 */
#include <algorithm>
#include <cstddef>
#include <map>
#include <queue>
class Solution {
  public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param root TreeNode类
     * @return int整型
     */
    int res = 0;
    map<int,int> m;
    int widthOfBinaryTree(TreeNode* root) {
        // write code here
        if(root==nullptr)    return res;
        dfs(root,0,0);
        return res;
    }
    void dfs(TreeNode* root,int depth,int index){
        if(root==nullptr) return;
        if(m.find(depth)==m.end()) m.insert(pair<int,int>(depth,index));
        res=max(res,index-m.at(depth)+1);
        dfs(root->left, depth+1, index*2);
        dfs(root->right,depth+1,index*2+1);

    }
};
全部评论

相关推荐

不愿透露姓名的神秘牛友
09-11 10:08
点赞 评论 收藏
分享
08-08 16:33
唐山学院 Java
职场水母:首先,简历太长,对于实习和应届找工作,hr一眼扫的是学历,技术看实习,你写的技术栈字太多了,尽量用一句话概括不用写那么详细,技术面的时候会问的,而且技术栈都会在实习或者项目里体现,你要做的是,把你的简历浓缩为一页,删除没用的东西,比如实践经历,自我评价,这些纯废话,没用,专业技能写的太离谱,你真的熟练掌握了吗,建议都写熟悉,找工作和写论文不一样,追求的是干练和实用,把实习经历和项目提前,把掌握的技术栈写到最后,然后去找实习,
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务