当前位置:网站首页>【leetcode】199. Right view of binary tree
【leetcode】199. Right view of binary tree
2022-04-23 10:30:00 【Front end corner】
【leetcode】199. Right side view of binary tree

subject
Given a binary tree The root node root, Imagine yourself on the right side of it , In order from top to bottom , Returns the value of the node as seen from the right .
Example 1:
Input : [1,2,3,null,5,null,4]
Output : [1,3,4]
Example 2:
Input : [1,null,3]
Output : [1,3]
Example 3:
Input : []
Output : []
Tips :
The range of the number of nodes in a binary tree is [0,100]
-100 <= Node.val <= 100
Ideas
- Think about it , Look from right to left , What are the characteristics of the nodes you see ? Each layer can see only one node , And this node is the last node of each layer .
- Based on the above analysis , Obviously, the sequence traversal of binary tree is enough. When the last node of each layer is encountered , Just add the node to the result array .
Code
/** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } */
/** * @param {TreeNode} root * @return {number[]} */
var rightSideView = function(root) {
if(!root) return []
let queue = [root]
let res = []
while(queue.length){
const len = queue.length
for(let i = 0 ; i < len ; i++){
const curNode = queue.shift()
if(i === len - 1) res.push(curNode.val) // Determine whether to be the last node of the layer , If so, save it
if(curNode.left) queue.push(curNode.left)
if(curNode.right) queue.push(curNode.right)
}
}
return res
};
Complexity
- Time complexity : O ( n ) O(n) O(n)
- Spatial complexity : O ( n ) O(n) O(n)
Pay attention to my special column , Update three times a day leetcode Answer key , Get stronger together !
版权声明
本文为[Front end corner]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231027441251.html
边栏推荐
- 图像处理——噪声小记
- 得到知识服务app原型设计比较与实践
- App. In wechat applet JS files, components, APIs
- SQL Server 游标循环表数据
- 第一章 Oracle Database In-Memory 相关概念(IM-1.1)
- Charles function introduction and use tutorial
- Sim Api User Guide(7)
- Jerry's users how to handle events in the simplest way [chapter]
- 101. Symmetric Tree
- Juc并发编程06——深入剖析队列同步器AQS源码
猜你喜欢
Solution architect's small bag - 5 types of architecture diagrams
Sim Api User Guide(6)
[provincial election joint examination 2022 d2t1] card (state compression DP, FWT convolution)
Exercise questions and simulation test of refrigeration and air conditioning equipment operation test in 2022
Swagger2 接口如何导入Postman
101. Symmetric Tree
Jerry's more accurate determination of abnormal address [chapter]
C语言——自定义类型
Example of pop-up task progress bar function based on pyqt5
Shell script interaction free
随机推荐
707. Design linked list (linked list)
得到知识服务app原型设计比较与实践
shell脚本免交互
ansible 云计算 自动化
JVM——》常用命令
得到知识服务app原型设计比较与实践
203. Remove linked list elements (linked list)
1、两数之和(哈希表)
mysql同一个表中相同数据怎么合并
Understand the new economic model of platofarm and its ecological progress
通过流式数据集成实现数据价值(5)- 流分析
1. Sum of two numbers (hash table)
What are Jerry's usual program exceptions? [chapter]
24、两两交换链表中的节点(链表)
Chapter 2 Oracle database in memory architecture (I) (im-2.1)
SSH uses private key to connect to server without key
LeetCode-608. Tree node
MapReduce compression
DBA common SQL statements (3) - cache, undo, index and wait events
Realizing data value through streaming data integration (5) - flow analysis