Skip to main content
01234Valid Tree?
mediumNeetCode 150

Graph Valid Tree

You're checking whether a set of connections forms a proper tree—one connected piece with no loops. It's a favorite in interviews because it forces you to articulate what makes a tree a tree, then verify those properties cleanly. The classic gotcha is accidentally detecting cycles when you revisit your parent node. Once you're comfortable here, connected-component problems become much easier.

BFS / DFSGraphs

Learn this pattern

Graph Interview Questions

BFS explores by layers using a queue; DFS explores deeply using a stack or recursion -- the same traversal with a different container. Choose BFS for shortest-path in unweighted graphs and "minimum steps," DFS for counting components, islands, and tree recursion.

Coco
Fin

Meet your coaches

Talk through the problem while you code. Signed-in reps become prep memory for the next session.

Chat with Fin or Coco right now on mobile.

Start talking through the problem here. Switch to desktop when you're ready to code and run tests.

Start coaching session
1Validate Graph is a Treemedium
2Part 2
3Part 3

Start Part 1 to unlock your next challenge

Based onLeetCode

Desktop required

Sorry - mobile cannot run the editor and tests yet. Use the next step below or email yourself a link to continue on desktop.

Read the pattern guide

Graph Interview Questions gives you a useful next rep while you are still on your phone.