This problem looks busy at first, but the accepted solution is built around one steady invariant. For 783. Minimum Distance Between BST Nodes, the solution in this repository is mainly a two pointers solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: two pointers, sliding window.
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are inOrder, minDiffInBST.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01/**
02Given 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.
03
04Example :
05
06Input: root = [4,2,6,1,3,null,null]
07Output: 1
08Explanation:
09Note that root is a TreeNode object, not an array.
10
11The given tree [4,2,6,1,3,null,null] is represented by the following diagram:
12
13 4
14 / \
15 2 6
16 / \
17 1 3
18
19while the minimum difference in this tree is 1, it occurs between node 1 and node 2, also between node 3 and node 2.
20Note:
21
22The size of the BST will be between 2 and 100.
23The BST is always valid, each node's value is an integer, and each node's value is different.
24**/
25
26//Runtime: 4 ms, faster than 100.00% of C++ online submissions for Minimum Distance Between BST Nodes.
27//Memory Usage: 10.8 MB, less than 100.00% of C++ online submissions for Minimum Distance Between BST Nodes.
28
29/**
30 * Definition for a binary tree node.
31 * struct TreeNode {
32 * int val;
33 * TreeNode *left;
34 * TreeNode *right;
35 * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
36 * };
37 */
38class Solution {
39public:
40 //initialize for leftmost leaf,
41 //we want the first node->val - last to be very large
42 int last = INT_MIN/2;
43 int ans = INT_MAX; //need to find min
44
45 void inOrder(TreeNode* node){
46 if(node->left) inOrder(node->left);
47 ans = min(ans, node->val - last);
48 last = node->val;
49 if(node->right) inOrder(node->right);
50 }
51
52 int minDiffInBST(TreeNode* root) {
53 inOrder(root);
54
55 return ans;
56 }
57};
Cost