数据结构与算法之LeetCode-662. 二叉树最大宽度 -(DFS,BFS)

news/2024/7/3 14:59:12

662. 二叉树最大宽度 - 力扣(LeetCode)

  • 根节点的位置为1(index
  • 左子节点的位置就为1*2
  • 右子节点的位置就为1*2+1
    • 节点下标会非常大(2**3000)个子节点,超过JS的number范围,因此需要使用bigint避免溢出
var widthOfBinaryTree = function(root) {
   // bfs
  const queue = [[root,1n]];
  let max = -1;
  while(queue.length){
    const size = queue.length;
    max = Math.max(max,Number(queue[queue.length-1][1]-queue[0][1]+1n));
    for(let i=0;i<size;i++){
      const [node,index] = queue.shift();
      if(node.left){
         queue.push([node.left,index*2n])
      }
      if(node.right){
        queue.push([node.right,index*2n+1n])
      }
      
    }
  }
  return max
};

执行结果:通过

执行用时:60 ms, 在所有 JavaScript 提交中击败了98.72%的用户

内存消耗:47.1 MB, 在所有 JavaScript 提交中击败了31.91%的用户

通过测试用例:114 / 114

function widthOfBinaryTree(root: TreeNode | null): number {
    const queue: [TreeNode, bigint][] = [[root, 1n]]
    let max = -1
    while (queue.length) {
        const size = queue.length
        max = Math.max(max, Number(queue[queue.length - 1][1] - queue[0][1] + 1n))
        for (let i = 0; i < size; i++) {
            const [node, index] = queue.shift()
            node?.left && queue.push([node.left, index * 2n])
            node?.right && queue.push([node.right, index * 2n + 1n])
        }
    }
    return max
};
var widthOfBinaryTree = function(root) {
    /** JS 存在计数溢出的问题,使用 BigInt,BigInt 不能调用 Math 中的方法。 */
    let maxWidth = 1n;
    const leftIds = []
    const dfs = (root, level, currIdx) => {
        if (leftIds[level] === undefined) {
            leftIds[level] = currIdx;
        } else {
            const width = currIdx - leftIds[level] + 1n;
            maxWidth = maxWidth > width ? maxWidth : width;
        }
        if (root.left !== null) {
            dfs(root.left, level + 1, currIdx * 2n - 1n);
        }
        if (root.right !== null) {
            dfs(root.right, level + 1, currIdx * 2n);
        }
    }
    dfs(root, 0, 1n);
    return maxWidth;    
};

执行结果:通过

执行用时:80 ms, 在所有 JavaScript 提交中击败了32.55%的用户

内存消耗:45.9 MB, 在所有 JavaScript 提交中击败了55.89%的用户

通过测试用例:114 / 114

参考链接

662. 二叉树最大宽度 - 力扣(LeetCode)

二叉树最大宽度 - 二叉树最大宽度 - 力扣(LeetCode)

【wendraw】BFS 的简单应用 - 二叉树最大宽度 - 力扣(LeetCode)

[662. 二叉树最大宽度 - 二叉树最大宽度 - 力扣(LeetCode)](


http://www.niftyadmin.cn/n/4426640.html

相关文章

运维未来的发展方向是智能运维

近年来运维技术飞速发展&#xff0c;运维团队大多建设好了各种系统&#xff0c;虚拟化、容器化、持续集成等等。但是如何有效的利用这些系统最终实现站点的高可用、高性能、高可扩展&#xff1f;随着智能化技术的发展&#xff0c;为了解决上述运维领域的问题&#xff0c;智能运…

ocilib库的使用

1、ocilib库的下载:http://vrogier.github.io/ocilib/ 2、ocilib库参考手册:http://vrogier.github.io/ocilib/doc/html/index.html 3、ocilib库windows下的配置使用(下面说明来自于官方文件): Using OCILIB on Microsoft Windows OCILIB distribution packages provi…

数据结构与算法之LeetCode-652. 寻找重复的子树

652. 寻找重复的子树 - 力扣&#xff08;LeetCode&#xff09; /*** Definition for a binary tree node.* function TreeNode(val, left, right) {* this.val (valundefined ? 0 : val)* this.left (leftundefined ? null : left)* this.right (rightundefi…

N-122基于springboot,vue网上订餐系统

开发工具&#xff1a;IDEA 服务器&#xff1a;Tomcat9.0&#xff0c; jdk1.8 项目构建&#xff1a;maven 数据库&#xff1a;mysql5.7 前端技术 &#xff1a;VueElementUI 服务端技术&#xff1a;springbootmybatisredis 本系统分用户前台和管理后台两部分&#xff0c;…

ocilib操作 long raw类型的字段

1、理解long、long raw、clob、blob的区别: (1)、long用来存储可边长度“字符串”,最大长度是2GB,对于超出一定长度的文本,只能用long类型来存储,并且一个表只能包含一个long类型; (2)、long raw用于存储二进值数据,最大2GB,并且一个表只能包含一个long raw类型;…

RedLeaves和PlugX恶意软件是如何工作的?

国家网络安全和通信集成中心了解到针对各个垂直行业的多种恶意软件植入&#xff0c;包括RedLeaves和PlugX。这些恶意软件是如何工作的?我们该如何应对? Judith Myerson&#xff1a;攻击者利用系统管理员的身份启动多种恶意软件&#xff0c;包括RedLeaves和PlugX。它们使用开放…

ocilib操作CLOB字段

1、理解long、long raw、clob、blob的区别: (1)、long用来存储可边长度“字符串”,最大长度是2GB,对于超出一定长度的文本,只能用long类型来存储,并且一个表只能包含一个long类型; (2)、long raw用于存储二进值数据,最大2GB,并且一个表只能包含一个long raw类型;…

数据结构与算法之LeetCode-655. 输出二叉树 - 力扣(LeetCode)

655. 输出二叉树 - 力扣&#xff08;LeetCode&#xff09; 先构建高度 height在构建层数&#xff0c;根据高度和层数得到数的位置 DFS /*** Definition for a binary tree node.* function TreeNode(val, left, right) {* this.val (valundefined ? 0 : val)* thi…