-
-
Notifications
You must be signed in to change notification settings - Fork 737
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[LeetCode] 199. Binary Tree Right Side View #199
Comments
推荐一下discussion里面c++ 高票解法, 利用变形前序访问来求得right view. 变量level设置的非常巧妙
|
楼主,我想问一个有可能听起来很蠢的想法,为什么不能直接node.right遍历,把每个val放到res中,直到node.right为空?这样做感觉好像没有问题,但是写出来就TLE |
如果左子树的深度大于右子树,这样可能不行,我一开始也是按照你这思路写,倒是没有TLE,只是OJ过不了 |
这种情况,如果一直dfs(node.right)来遍历右子树,就没办法捉到左子节点3了。 |
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
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.
For example:
Given the following binary tree,
You should return
[1, 3, 4]
.Credits:
Special thanks to @amrsaqr for adding this problem and creating all test cases.
这道题要求我们打印出二叉树每一行最右边的一个数字,实际上是求二叉树层序遍历的一种变形,我们只需要保存每一层最右边的数字即可,可以参考我之前的博客 Binary Tree Level Order Traversal 二叉树层序遍历,这道题只要在之前那道题上稍加修改即可得到结果,还是需要用到数据结构队列queue,遍历每层的节点时,把下一层的节点都存入到queue中,每当开始新一层节点的遍历之前,先把新一层最后一个节点值存到结果中,代码如下:
LeetCode All in One 题目讲解汇总(持续更新中...)
The text was updated successfully, but these errors were encountered: