Problem · Tree

Find Leaves of Binary Tree

Learn this problem
MediumAtlassian logoAtlassianFULLTIMEOA

Problem statement

Repeatedly remove every current leaf from a binary tree. Return one row per removal round; values within a row follow their left-to-right postorder discovery order.

Function

findLeaves(root: TreeNode) → int[][]

Examples

Example 1

root = [1,2,3,4,5]return = [[4,5,3],[2],[1]]

The first round removes 4,5,3, then 2, then the root.

Constraints

  • The tree contains between 1 and 10000 nodes.
  • -10^9 <= Node.val <= 10^9

More Atlassian problems

drafts saved locally
public int[][] findLeaves(TreeNode root) {
  // Write your code here.
}
root[1,2,3,4,5]
expected[[4,5,3],[2],[1]]
checking account