• LeetCode每日一题:662二叉树最大宽度


    题目描述:

      给定一个二叉树,编写一个函数来获取这个树的最大宽度。树的宽度是所有层中的最大宽度。这个二叉树与满二叉树(full binary tree)结构相同,但一些节点为空。

      每一层的宽度被定义为两个端点(该层最左和最右的非空节点,两端点间的null节点也计入长度)之间的长度。

    解题原理:

      宽度优先遍历每个节点,纪录每个节点的位置信息。比如当前节点位置是n  那么它的左孩子位置就是2*n 它的右孩子就是2*n+1

      该层的节点个数就是:该层的最右节点位置-该层的最右节点位置+1  最右节点指的是该层最右的非空节点。

      由于空的节点也有可能算,所以空节点也要封装成信息加入队列。

    class Solution {
        public int widthOfBinaryTree(TreeNode root) {
            Queue<Info> queue = new LinkedList<>();
            queue.add(new Info(root,1,1));
            int left = 1;
            int max = 0;
            int level = 1;
            while(!queue.isEmpty()){
                Info curNodeInfo = queue.poll();
                TreeNode curNode = curNodeInfo.node;
                int position = curNodeInfo.position;
                int curLevel = curNodeInfo.level;
                if(curNodeInfo.node!=null){
                    queue.add(new Info(curNode.left,2*position,curLevel+1));
                    queue.add(new Info(curNode.right,2*position+1,curLevel+1));
                    if(level!=curLevel){
                // 说明进入了下一层,所以当前节点位置就是最左位置。纪录一下 level
    = curLevel; left = position; }
              // 每次都拿 当前节点位置-最左节点位置+1 并更新,总会遍历到最右的非空节点。 max
    = Math.max(max,position-left+1); } } return max; } // 纪录节点信息:节点、位置、该节点所在层数 static class Info{ TreeNode node; int position; int level; public Info(TreeNode node,int position,int level){ this.node = node; this.position = position; this.level = level; } } }

    延伸题目:

      求二叉树最大宽度,不包含空节点

    解题原理:

      准备一个HashMap<节点,层数>

      准备两个变量:curLevel(当前层数) nodes(当前层数的节点数) 进队列时候,向HashMap中put(节点,当前节点层数)

      出队列时候,在HashMap中查询该节点层数,并nodes++ 当层数变大时,nodes重置

    public int maxWidth(TreeNode root) {
            HashMap<TreeNode,Integer> map = new HashMap<>();
            map.put(root,1);
            Queue<TreeNode> queue = new LinkedList<>();
            queue.add(root);
            int level = 1;
            int nodeCount = 0;
            int max = 0;
            while(!queue.isEmpty()){
                TreeNode curNode = queue.poll();
                int curLevel = map.get(curNode);
                if(curLevel==level){
                    nodeCount++;
                }else {
                    max = Math.max(nodeCount,max);
                    level = curLevel;
                    nodeCount = 1;
                }
                if(curNode.left!=null){
                    queue.add(curNode.left);
                    if(!map.containsKey(curNode.left)){
                        map.put(curNode.left,curLevel+1);
                    }
                }
                if(curNode.right!=null){
                    queue.add(curNode.right);
                    if(!map.containsKey(curNode.right)){
                        map.put(curNode.right,curLevel+1);
                    }
                }
    
            }
            max = Math.max(max,nodeCount);
            return max;
        }
  • 相关阅读:
    教师派10
    教师派9
    简单理解socket(AF_INET&SOCK_STREAM,SOCK_DGRAM)
    Deepin安装MySQL(MariaDB)不提示设置密码问题(密码为空)
    经典排序算法-附python代码
    Linux虚拟环境virtualevn
    linux安装虚拟环境
    deepin安装虚拟环境virtualenv
    deepin安装虚拟环境virtualenv
    把握AFNet网络请求完成的正确时机
  • 原文地址:https://www.cnblogs.com/lxy-java/p/13191654.html
Copyright © 2020-2023  润新知