0
Leetcode Problem 366. Find Leaves of Binary Tree
366. Find Leaves of Binary Tree
AI Mock Interview
Leetcode Solutions
DFS (Depth-First Search) without sorting
Solution Idea
Algorithm Steps
Code Implementation
Complexity Analysis
Define a recursive function
getHeight(node)
that returns the height of the given node.
If the node is
null
, return -1.
Calculate the height of the left and right children recursively.
The height of the current node is 1 plus the maximum height of its children.
Add the current node's value to the list corresponding to its height in the output list of lists.
If the list for the current height does not exist, create it.
Return the height of the current node.
Call
getHeight(root)
to initiate the process.
Return the output list of lists.
DFS (Depth-First Search) with sorting
Ask Question
Programming Language
Purpose:
General Question
Debug My Code
image/screenshot of info
(optional)
[+]
Full Screen
Loading...
Get Answer
Suggested Answer
Answer
Full Screen
Copy Answer Code
Loading...