Test If A Binary Tree Is Height Balanced ('Balanced Binary Tree' on LeetCode)

preview_player
Показать описание
📹 Intuitive Video Explanations
🏃 Run Code As You Learn
💾 Save Progress
❓New Unseen Questions
🔎 Get All Solutions

Question: Write a program that takes the root of a binary tree as input and checks whether the tree is height-balanced.

A tree is height balanced if for each node in the tree, the difference in the height of its left and right subtrees is at most one.

Approach 1 (Get Height Of Tree Rooted At Each Node)

We can perform a traversal of the tree and at each node get the height of its left and right subtrees.

This wastes time as we will be repeating work and the traversal of nodes.

Approach 2 (Drill Down With Recursion And Respond Back Up)

We can notice that we don't need to know the heights of all of the subtrees all at once.

All we need to know is whether a subtree is height balanced or not and the height of the tree rooted at that node, not information about any of its descendants.

Our base case is that a null node (we went past the leaves in our recursion) is height balanced and has a height of -1 since it is an empty tree.

So the key is that we will drive towards our base case of the null leaf descendant and deduce and check heights on the way upwards.

Key points of interest:

1.) Is the subtree height balanced?
2.) What is the height of the tree rooted at that node?

Complexities

Time: O( n )

This is a postorder traversal (left right node) with possible early termination if any left subtree turns out unbalanced and an early result bubbles back up.

At worst we will still touch all n nodes if we have no early termination.

Space: O( h )

Our call stack (from recursion) will only go as far deep as the height of the tree, so h (the height of the tree) is our space bound for the amount of call stack frames that we will create

++++++++++++++++++++++++++++++++++++++++++++++++++

++++++++++++++++++++++++++++++++++++++++++++++++++

This question is number 10.1 in "Elements of Programming Interviews" by Adnan Aziz, Tsung-Hsien Lee, and Amit Prakash.
Рекомендации по теме
Комментарии
Автор

Table of Contents:

The Problem Introduction 0:00 - 0:33
Cases That Are Height Balanced 0:33 - 1:56
Cases That Are NOT Height Balanced 1:56 - 2:58
Approach #1: Get Heights of Subtrees At Each Node 2:58 - 3:46
Approach #2: Recurse To Base Cases 3:46 - 4:23
Walkthrough of The Recursion 4:23 - 12:39
Time Complexity 12:39 - 13:25
Space Complexity 13:25 - 13:39
Wrap Up 13:39 - 13:57

The teacher's notes contain a link to the code for the problem discussed in the video. It is fully commented for teaching purposes strictly.

BackToBackSWE
Автор

Me on Tinder: Hey, what is your height? And are you balanced?

BismaSuleman
Автор

Sharing this channel to all my friends who are interested to learn data structure algorithm .. Please make more videos on Data Structure and algorithms .. Believe me nobody tech like you. This channel has potential to become one of the best. The difference between you and others, is that most people just jumps directly into the solution but you tell us 'the thought process', 'how to interpret the problem' which are most important. Your think loud approach is best part. Please don't stop making such video. People like me are always with you.

stephyjacob
Автор

Better than my data structures professor, thank you

rban
Автор

OMG the way you explain the idea behind why the algorithm works, it just blew me away. Thanks a lot mate!

nikhilkumarmishra
Автор

Thanks for the videos! They're very well done compared to most of the others where people either start writing code immediately or jump straight to the solution without explaining how they got there.

PherricOxide
Автор

You deserve way way more recognition and credit for the work you have done sir!
better than any college professor I have had.

adityajain-fnne
Автор

Loved that idea of "asking a question".Thanks man!!

psthakur
Автор

you get so into explaining this, gotta love the head scratch lmao
thanks for an awesome explanation :D

missrockinout
Автор

great way of explaining stuff...Keep up the good work...you folks are as valuable as nation's best teachers.

afsinyilmaz
Автор

asking the "critical question" and then returning the answer to that question to my parent is a great way to reason about recursion in general
your teaching style sir is on another level!!!

josephwong
Автор

awesome work bro!! helped a lot in visualization of recursion calls

SOURAVKUMAR-twds
Автор

you're a remarkable teacher. blew my PhD data structures professor out of the water, honestly. thank you for making these videos. I'm surviving interview season bc of this.

turnuptheMIKEG
Автор

Your explanation is the best i have ever seen and really helps to understand these difficult problems. I appreciate your selfless work and the dedication with which you teach us these topics..Got to learn a lo from you, keep uploading more videos and soon this channel would turn out to be the best resource for interview preparation.

deepamkumar
Автор

you are so gifted at teaching, never stop!

ruthylevi
Автор

Thank you man! This video really helps me to understand the process of solving the problem.

quirkyquester
Автор

Thank you very much. I myself teach myself to code, I'm also a teacher in kindergarten. I recognize a lot of myself in you. Keep up the good work!

johnmcway
Автор

undoubtedly, the best explanation of how recursion works in trees

priyogopalsingha
Автор

I am so glad that I found your channel on Youtube...! Thank You very Much Sir!

UHemanthaKumar
Автор

What a smart and attractive illustration, well-done man!

kunpeng