Find Leaves of Binary Tree

Given the root of a binary tree, collect a tree's nodes as if you were doing this:

  • Collect all the leaf nodes.
  • Remove all the leaf nodes.
  • Repeat until the tree is empty.

Preorder traversal is better approach to this problem as we need to collect leaf nodes and remove those leaf nodes.

Solution is to write a utility function to collect and remove leaf nodes. Then call this function in a loop till the leaves of root node are null.

--

--

www.linkedin.com/in/jyothivs

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store