leetcode 530. ツリーの最小絶対差(python)

947 ワード

すべてのノードが負でない二叉探索ツリーを与え、ツリー内の任意の2つのノードの差の絶対値の最小値を求める.
例:
  :

   1
    \
     3
    /
   2

  :
1

  :
      1,   2   1         1(   2   3)。

注意:ツリーには少なくとも2つのノードがあります.
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def getMinimumDifference(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        self.val = None
        self.ans = float("inf")
        def inorder(root):
            if not root:
                return
            inorder(root.left)
            if self.val is not None:
                self.ans = min(self.ans, abs(root.val - self.val))
            self.val = root.val
            inorder(root.right)
        inorder(root)
        return self.ans