leetocde199. 二叉树的右视图python
2021/11/22 17:12:04
本文主要是介绍leetocde199. 二叉树的右视图python,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
题目描述:
题解:递归
参考了评论区的思路
1.递归终止条件:输入root节点为空。
2.递归返回值:保存右侧节点值的列表。
3.当前轮递归:判断输入root节点是否为当前层最右侧的节点,如果是就加入列表。否则对root的右子节点和左子节点递归调用。
class Solution(object): def rightSideView(self, root): rightres = [] depth = 0 def repeat(root,depth): if root==None: return if depth==len(rightres): rightres.append(root.val) depth=depth+1 repeat(root.right,depth) repeat(root.left, depth) repeat(root,depth) return rightres
这篇关于leetocde199. 二叉树的右视图python的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python