bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 783. Minimum Distance Between BST Nodes

783. Minimum Distance Between BST Nodes

Leetcode Solutions

In-order Traversal Without List

  1. Initialize minDistance to MAX_VALUE and prevValue to None.
  2. Define a recursive function inOrder that takes a node as an argument.
  3. If the node is None, return immediately.
  4. Recursively call inOrder on the left child of the node.
  5. If prevValue is not None, update minDistance with the minimum of the current minDistance and the difference between the node's value and prevValue.
  6. Set prevValue to the current node's value.
  7. Recursively call inOrder on the right child of the node.
  8. Call inOrder on the root node to start the traversal.
  9. Return minDistance after the traversal is complete.
UML Thumbnail

In-order Traversal with List

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...