1. 程式人生 > >二叉樹--leetcode 783.Minimum Distance Between BST Nodes

二叉樹--leetcode 783.Minimum Distance Between BST Nodes

https://www.cnblogs.com/robsann/p/7567596.html

https://blog.csdn.net/qq_33243189/article/details/80222629

兩篇講解二叉樹比較好的文章

二叉樹的中序遍歷--python3

783. Minimum Distance Between BST Nodes

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def minDiffInBST(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        res = []
        def res_list(root):
            if root == None:
                return []
            res_list(root.left)
            res.append(root.val)
            res_list(root.right)
        res_list(root) #中序遍歷函式
        result1 = []
        
        for i in range(len(res) - 1):
            result1.append(res[i+1] - res[i])
        print(result1)
            
        return min(result1)