【LEETCODE】 Find Largest Value in Each Tree Row javascript实现
来源:互联网 发布:树上10只鸟 程序员 编辑:程序博客网 时间:2024/06/10 00:19
leetcode 515题的要求是:
You need to find the largest value in each row of a binary tree.
Example:
Input:
1 / \ 3 2 / \ \ 5 3 9
Output: [1, 3, 9]
Subscribe to see which companies asked this question.
就是说给你一棵树,并且计算出所有的树每一层的中数据的最大值。
那么采用javascript的版本的解答如下(写法可能有代码冗余)
采用队列方式来解决,将每层的节点加入队列,并计算每层树节点的最大值,当节点没有叶子节点时,停止计算输出队列
/** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } *//** * @param {TreeNode} root * @return {number[]} */var largestValues = function(root) { var number=[]; var queue=Array(); var max=0; if(root===null) return number; queue.push(root); while(queue.length!==0) { var size=queue.length; var max=queue[0].val; for(var i=0;i<size;i++) { var node=queue[0]; queue.splice(0,1); var max=Math.max(max,node.val); if(node.left!==null) { queue.push(node.left); } if(node.right!==null) { queue.push(node.right); } } number.push(max); } return number;};
0 0
- 【LEETCODE】 Find Largest Value in Each Tree Row javascript实现
- LeetCode 515. Find Largest Value in Each Tree Row
- LeetCode OJ 515. Find Largest Value in Each Tree Row
- [leetcode 515]Find Largest Value in Each Tree Row
- LeetCode笔记:515. Find Largest Value in Each Tree Row
- [leetcode] 515. Find Largest Value in Each Tree Row
- 【LeetCode】515. Find Largest Value in Each Tree Row
- LeetCode;515. Find Largest Value in Each Tree Row
- LeetCode #515: Find Largest Value in Each Tree Row
- leetcode 515. Find Largest Value in Each Tree Row
- leetcode 515 Find Largest Value in Each Tree Row C++
- leetcode 515. Find Largest Value in Each Tree Row
- 【LeetCode】Find Largest Value in Each Tree Row 解题报告
- leetcode-515. Find Largest Value in Each Tree Row
- leetcode 515. Find Largest Value in Each Tree Row
- leetcode 515. Find Largest Value in Each Tree Row
- leetcode 515. Find Largest Value in Each Tree Row
- Leetcode 515 Find Largest Value in Each Tree Row
- Angular企业级开发(8)-控制器的作用域
- 大数据平台cloudera manager安装
- C语言perror( )函数
- 设计模式之桥接模式
- Java集合
- 【LEETCODE】 Find Largest Value in Each Tree Row javascript实现
- 左偏树/斜堆/可并堆-洛谷P3377 【模板】左偏树(可并堆)
- Practice3_7_vector_sort_struct_gold_silver_bronze_playerName3
- HOJ 2201 题解
- 安装caffe(CPU版)遇到的问题
- hibernate之constrained详解
- 文章标题 POJ 1151 : Atlantis (线段树+扫描线)
- 有感而发
- 继续学习之Gizmos in the Scene View的自己写的一个无聊的效果