632. 二叉树的最大节点

632. 二叉树的最大节点

中文English

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

样例

样例1:

输入:
{1,-5,3,1,2,-4,-5}
输出: 3
说明:
这棵树如下所示:
     1
   /   
 -5     3
 /    /  
1   2 -4  -5

样例 2

输入:
{10,-5,2,0,3,-4,-5}
输出: 10
说明:
这棵树如下所示:
     10
   /   
 -5     2
 /    /  
0   3 -4  -5 

遍历
"""
Definition of TreeNode:
class TreeNode:
    def __init__(self, val):
        self.val = val
        self.left, self.right = None, None
"""

class Solution:
    """
    @param: root: the root of tree
    @return: the max node
    """
    def __init__(self):
        self.max_num = -sys.maxsize
        self.node = None
    
    def maxNode(self, root):
        # write your code here
        if not root:
            return None 
        
        if root.val > self.max_num:
            self.max_num = root.val
            self.node = root
        
        self.maxNode(root.left)
        self.maxNode(root.right)
        
        return self.node
原文地址:https://www.cnblogs.com/yunxintryyoubest/p/14171009.html