题目地址
LeetCode#783 Minimum Distance Between BST Nodes
题目描述
Given a Binary Search Tree (BST) with the root node root
, return the minimum difference between the values of any two different nodes in the tree.
Example :
1 | Input: root = [4,2,6,1,3,null,null] |
Note:
- The size of the BST will be between 2 and
100
. - The BST is always valid, each node’s value is an integer, and each node’s value is different.
解题思路
刚开始想复杂了。。
直接将树的所有节点值存入数组并排序,然后找到相差最小的数字返回其差值。
解题代码【.CPP】
1 | /** |