Skip to the code
cf.
+10 XP

#448 Is the Org Chart a Tree?

easy

An org chart tool imports reporting lines from a spreadsheet. People are numbered 0 to n - 1, and each lines[i] = [a, b] is a reporting line between a and b (direction doesn't matter here).

The chart is valid only if the lines form a tree: everyone is connected, and there are no loops. Write isTree(n, lines) that returns true if they do and false otherwise.

Example 1

Input: n = 4, lines = [[0,1],[0,2],[2,3]]
Output: true

Everyone is connected through 3 lines, with no loop.

Example 2

Input: n = 4, lines = [[0,1],[1,2],[2,0]]
Output: false

0, 1, and 2 form a loop, and person 3 isn't connected at all.

Constraints

  • 1 <= n <= 10000
  • 0 <= lines.length <= 20000
  • 0 <= a, b < n
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).