leetcode-二叉树右视图(python)
2019-03-24 12:21
453 查看
题目:
给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
示例:
输入: [1,2,3,null,5,null,4]
输出: [1, 3, 4]
解释:
1 <—
/
2 3 <—
\
5 4 <—
**思想:**二叉树层次遍历,取出每一层最后一个
代码:
class Solution: def rightSideView(self, root: TreeNode) -> List[int]: if not root: return [] res=[] #存储最后结果 curnode=[root] #存储当先遍历层数的node nexnode=[] #存储下一层的node res.append(curnode[0].val) while curnode: for s in curnode: if s.right: #先存储右节点 nexnode.append(s.right) if s.left: nexnode.append(s.left) if nexnode: #再存储左节点 res.append(nexnode[0].val) curnode=nexnode nexnode=[] return res
相关文章推荐
- 利用python 完成 leetcode 199 二叉树的右视图
- 利用python 完成leetcode 114 二叉树展开为链表
- LeetCode 331. Verify Preorder Serialization of a Binary Treet 二叉树前序遍历验证 Python Solution
- LeetCode--Maximum Depth of Binary Tree (检索二叉树的最大深度)Python
- Leetcode107. 二叉树的层次遍历 II(python3)
- LeetCode--102. Binary Tree Level Order Traversal(二叉树水平遍历)Python
- leetcode | 按层访问一棵二叉树 | Python
- LeetCode 105,106. Construct Binary Tree 重建二叉树 Python Solution
- LeetCode 144. 二叉树的前序遍历 Python
- LeetCode 623 在二叉树中增加一行 Python
- leetcode 第105题(从前序与中序遍历序列构造二叉树) ,第106题(从中序与后序遍历序列构造二叉树)python解法(用时40ms)
- leetcode199_二叉树的右视图
- C++Leetcode199:二叉树的右视图
- [LeetCode] 199. Binary Tree Right Side View 二叉树的右侧视图
- LeetCode199. 二叉树的右视图
- [LeetCode] Binary Tree Right Side View 二叉树的右侧视图
- LeetCode--Merge Two Binary Trees(合并两个二叉树)Python
- LeetCode--Invert Binary Tree (反转二叉树)Python
- leetcode 199. Binary Tree Right Side View-树的右视图|递归|非递归|Java|Python
- Leetcode 199 Binary Tree Right Side View 二叉树右视图