leetcode 662.二叉树最大深度 Java

题目链接

https://leetcode-cn.com/problems/maximum-width-of-binary-tree/

描述

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

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

注意: 答案在32位有符号整数的表示范围内。

示例

示例 1:

输入: 

           1
         /   \
        3     2
       / \     \  
      5   3     9 

输出: 4
解释: 最大值出现在树的第 3 层,宽度为 4 (5,3,null,9)

示例 2:

输入: 

          1
         /  
        3    
       / \       
      5   3     

输出: 2
解释: 最大值出现在树的第 3 层,宽度为 2 (5,3)

示例 3:

输入: 

          1
         / \
        3   2 
       /        
      5      

输出: 2
解释: 最大值出现在树的第 2 层,宽度为 2 (3,2)

示例 4:

输入: 

          1
         / \
        3   2
       /     \  
      5       9 
     /         \
    6           7
输出: 8
解释: 最大值出现在树的第 4 层,宽度为 8 (6,null,null,null,null,null,null,7)

初始代码模板

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int widthOfBinaryTree(TreeNode root) {
        
    }
}

模板

我用的两个队列,一个保存节点,一个保存下标,基本就是层序遍历的内容。
还可以去看看下面的题解,直接在原二叉树修改节点:
https://leetcode-cn.com/problems/maximum-width-of-binary-tree/solution/javashuang-bai-yi-chong-you-dian-tou-ji-qu-qiao-de/

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int widthOfBinaryTree(TreeNode root) {
        if (root == null) {
            return 0;
        }

        LinkedList<TreeNode> nodeQueue = new LinkedList<>();
        LinkedList<Integer> posQueue = new LinkedList<>();
        int ans = 0;

        nodeQueue.offer(root);
        posQueue.offer(0);

        while (!nodeQueue.isEmpty()) {
            ans = Math.max(ans, posQueue.getLast() - posQueue.getFirst() + 1);

            for (int size = nodeQueue.size(); size > 0; size--) {
                TreeNode cur = nodeQueue.poll();
                int curPos = posQueue.poll();

                if (cur.left != null) {
                    nodeQueue.offer(cur.left);
                    posQueue.offer(curPos * 2 + 1);
                }

                if (cur.right != null) {
                    nodeQueue.offer(cur.right);
                    posQueue.offer(curPos * 2 + 2);
                }
            }
        }

        return ans;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值