程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2023-05(1)

2023-06(2)

leetcode刷题笔记(Golang)--199. Binary Tree Right Side View

发布于2020-02-24 23:55     阅读(1011)     评论(0)     点赞(22)     收藏(2)


  1. Binary Tree Right Side View
    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

Example:

Input: [1,2,3,null,5,null,4]
Output: [1, 3, 4]
Explanation:

1 <—
/
2 3 <—
\
5 4 <—

解题思路,栈从左到右存储每一层的node,然后弹出top node

func rightSideView(root *TreeNode) []int {
	if root == nil {
		return []int{}
	}
	res := []int{root.Val}
	q := []*TreeNode{root}
	for len(q) > 0 {
		lel := []*TreeNode{}
		for i := 0; i < len(q); i++ {
			if q[i].Left != nil {
				lel = append(lel, q[i].Left)
			}
			if q[i].Right != nil {
				lel = append(lel, q[i].Right)
			}
		}
        if len(lel) > 0{
           	res = append(res, lel[len(lel)-1].Val) 
        }
		q = lel
	}
	return res   
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
发布了117 篇原创文章 · 获赞 0 · 访问量 2442


所属网站分类: 技术文章 > 博客

作者:雪儿

链接:https://www.pythonheidong.com/blog/article/232912/7c92c8e2ab0ceae51d27/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

22 0
收藏该文
已收藏

评论内容:(最多支持255个字符)