1 Star 0 Fork 0

蓝桥云课/python-100

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
034-second_largest.py 847 Bytes
一键复制 编辑 原始数据 按行查看 历史
xiaoyi733112 提交于 2020-02-20 17:17 . python-100 answer
from bst import Bst
class Solution(Bst):
def find_second_largest(self):
if self.root is None:
raise TypeError('root cannot be None')
if self.root.right is None and self.root.left is None:
raise ValueError('root must have at least one child')
return self._find_second_largest(self.root)
def _find_second_largest(self, node):
if node.right is not None:
if node.right.left is not None or node.right.right is not None:
return self._find_second_largest(node.right)
else:
return node
else:
return self._find_right_most_node(node.left)
def _find_right_most_node(self, node):
if node.right is not None:
return self._find_right_most_node(node.right)
else:
return node
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/lanqiao-courses/python-100.git
git@gitee.com:lanqiao-courses/python-100.git
lanqiao-courses
python-100
python-100
master

搜索帮助