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

Leetcode Problem 671. Second Minimum Node In a Binary Tree

671. Second Minimum Node In a Binary Tree

Leetcode Solutions

Depth-First Search with Early Stopping

  1. Initialize two variables, min1 to root.val and ans to float('inf').
  2. Define a recursive function dfs(node) that will perform the depth-first search.
  3. Inside dfs(node), if the node is None, return immediately.
  4. If node.val is greater than min1 and less than ans, update ans to node.val.
  5. If node.val equals min1, recursively call dfs on both node.left and node.right.
  6. Call dfs(root) to start the DFS traversal from the root.
  7. After the traversal, if ans is still float('inf'), return -1 as there is no second minimum value. Otherwise, return ans.
UML Thumbnail

Collecting All Unique Values

Ask Question

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

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...