Binary Tree - 53: Check if all Leaf Nodes are at same level in Binary Tree

preview_player
Показать описание
Solution:
- Using Pre order traversal, our aim is to check if all the leaf nodes are at same level.
- We'll take a variable 'levelOfLeaf' & iterate the binary tree in preorder manner
- Whenever we find Leaf node, we'll check the level of Leaf node.
- At any point, if level if other than exiting one, we'll return false.

- Time Complexity: O(n)
- Space Complexity: O(1)

Do Watch video for more info

This Problem is synonym of following problems:
binary tree check if all leaf nodes are at same level,
check if all leaf nodes are at same level in binary tree,
coding simplified

CHECK OUT CODING SIMPLIFIED

★☆★ VIEW THE BLOG POST: ★☆★

I started my YouTube channel, Coding Simplified, during Dec of 2015.
Since then, I've published over 200+ videos.

★☆★ SUBSCRIBE TO ME ON YOUTUBE: ★☆★

★☆★ Send us mail at: ★☆★
Рекомендации по теме
Комментарии
Автор

Very nice solution. One solution i can think of would be to store all the leaf nodes levels as values inside a hashmap and return false if values are different. This will take an extra O(n) space but would work fine as well. 😅

biswajitsingh
Автор

why we had applied and in recursive call

yadnyeshrane
Автор

Why dont we stop as soon as we go to next level as multiple level means multiple leaf nodes at different level

narendratechguy
Автор

Sir I request to please run the code that you make in tutorials..

FarhanAli-kmid
Автор

why can we find max height from leaf and min height from leaf for each node and whenever we found any case of (lh!=rh) we return false;

MohitMaroliyaBCS
Автор

thnkssss u sooo much to provide easiest explanation

sharuk
Автор

Solve question
Min-heigth==max-height

dilipsuthar
Автор

can it be solved using level order traversal

hussainahmed
Автор

can we use this logic-> if height of both left and right subtrees are same then return true else return false? Or did i miss something?

siddharthsahu