LC-Minimum Absolute Difference in BST
程序员文章站
2022-03-07 18:21:19
...
# 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
"""
list = []
def dfs(node):
if not node:
return
else:
dfs(node.left)
list.append(node.val)
dfs(node.right)
dfs(root)
min = 10 ** 100
for i in range(len(list)-1):
if list[i+1] - list[i] < min:
min = list[i+1] - list[i]
return min
0,自己实现的代码
1,题目要求:
给出一个二叉搜索树,找到任意两个节点的绝对值差的最小值
2,自己的想法就是以中序遍历的形式,将该搜索二叉树的值提取到列表中,然后依次比较相邻两个值的差,即可达到题目的要求。
如果树不是搜索二叉树也无妨,遍历完成后对列表进行sort处理即可。
上一篇: 530. Minimum Absolute Difference in BST
下一篇: (Java)leetcode-530 Minimum Absolute Difference in BST (二叉搜索树的最小绝对差)
推荐阅读
-
530. Minimum Absolute Difference in BST
-
Leetcode每日一题:530.minimum-absolute-difference-in-bst(二叉搜索树的最小绝对值)
-
530. Minimum Absolute Difference in BST
-
LC-Minimum Absolute Difference in BST
-
(Java)leetcode-530 Minimum Absolute Difference in BST (二叉搜索树的最小绝对差)
-
LeetCode之路:530. Minimum Absolute Difference in BST
-
530 - 二叉搜索树的最小绝对差(minimum-absolute-difference-in-bst)
-
530. Minimum Absolute Difference in BST
-
LeetCode 530. Minimum Absolute Difference in BST
-
Leetcode 530. Minimum Absolute Difference in BST