您的位置:首页 > 编程语言 > Python开发

【LintCode 入门】632. 二叉树的最大节点

2018-01-15 13:48 225 查看
1.问题描述:

在二叉树中寻找值最大的节点并返回。

2.样例:

给出如下一棵二叉树:
1
/   \
-5     2
/ \   /  \
0   3 -4  -5

返回值为 
3
 的节点。

3.代码:

class Solution:
"""
@param: root: the root of tree
@return: the max node
"""
maxNum = -9999
node = None
def maxNode(self, root):
# write your code here
if root is None:
return None
self.max(root)
return self.node

def max(self,root):
# 递归,循环二叉树所有节点对象,将最大值的节点对象赋值给node
if root is None:
return None
if root.val > self.maxNum:
self.maxNum = root.val
self.node = root
self.max(root.left)
self.max(root.right)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  LintCode Python 二叉树