Test If A Binary Tree Is Symmetric ('Symmetric 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 to check if a binary tree is symmetric in structure as well as value.

A binary tree is symmetric if we can draw a vertical line down from the root and the left and right subtrees are mirrors of each other in structure and value.

The least this could be is O(n) time because we have to inspect all n nodes values to ensure that they conform to the tree being symmetric.

All we need to do is traverse the tree correctly with our recursion checking pairs for conformity.

The Conditions

If the root is null, the tree is symmetric.

Our checking function will take 2 nodes in to compare for symmetry.

Our Base Cases

If both nodes passed in are null, by default we have a symmetric tree.

If one node is null and the other is not, then we have incongruence, the tree is not symmetric since a pair failed.

If both nodes are null, then compare values. If values are equal, then good. Go left and go right.

How do we come up with this on the spot?

First recognize the base cases.

Then recognize we will need some way to compare pairs of nodes since that's what matters when checking for symmetry.

Even if this were an array that is how we would do it, pairs of indices.

Then recognize the way the recursive case must continue the work on the way down and your solution would look like this at the end.

Complexities

Time: O(n)

Space: O(h) (as we discussed, it can't really be O(n) worst case since
the first check would fail if the tree is skewed. The worst case IS O(h))

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

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

This problem is problem 10.2 in the book Elements of Programming Interviews by Adnan Aziz
Рекомендации по теме
Комментарии
Автор

Table of Contents:

Video Setup 0:00 - 0:18
Problem Introduction 0:18 - 2:45
Find The Pattern With An Array 2:45 - 4:20
Find The Pattern With A Binary Tree 4:20 - 6:48
The Code. 6:48 - 9:17
Time & Space Complexity 9:17 - 10:58
Apply This To Other Recursive Problems 10:58 - 12:05

As with every video, I'm not perfect, I hope I didn't make any gross mistakes in the video. If all is well, great, this is the explanation for how to test if a binary tree is symmetric.

BackToBackSWE
Автор

Please don't feel sorry for being loud, that's your speciality when you talk ;
it feels like a professional teacher is teaching and he wants all his students to pass in any cost.

yashshreeshinde
Автор

Your video is so good. The explanation is super clear.

smarchz
Автор

Awesome video! I think you decouple this problem quite elegantly. Trying to solve a problem in a recursive way is not easy. You did a great job!

frankchen
Автор

The energy in your explanation is just awesome, it makes everyone to listen your words with great concern, it is really true that positive person give off positive energy

soumyadeeproy
Автор

The best explanation. Even the bubbling up guys 😆 like nick white and kevin fades in front of you! Increased my confidence dude. Who the hack disliked!! Complete disguise!

vandit_quantum
Автор

thanks sir for the simplest explanation i have came across so far

digvijayyamagekar
Автор

very nice, you are the best youtuber who can explain leetcode problems clearly and make them easy to understand.

rukuanyang
Автор

Thank you! I've been watching MIT lectures on data structures and algorithms and when I don't understand something, I always get a super clear explanation here! You're terrific at explaining things

milipo
Автор

Thankss Dudeee!!! finally, after this video i solved this problem successfully.

muskantripathi
Автор

That was a fantastic explanation!!...It very well shows how passionate you are about your work...and believe me you can make people love data-stuctures..

ruchikadakalia
Автор

Watching on X-mas day after 2 years :)

anshulgupta
Автор

Damn, if my interviewer had your energy in this video, i'd be half pumped / half having an anxiety attack. But anyways, I'm on a binge on your videos, thanks for the help!

shtephl
Автор

Your editing has improved a ton! Keep up the good work.

dephcn
Автор

I needed you in my life and I didn't even know until now

LilyEvans
Автор

Woah your energy boosted me as well. Thanks!

vivekojha
Автор

lmao, "sustain our lives" i felt that

pyak
Автор

Thanks for this video I really like the way you explain this problem

lorenzovelasque
Автор

I think the space complexity is also O(n). The worst case is basically a symmetric list with the middle as the tree root. O(n/2) ≈ O(n), in terms of space for the stack frames

SamWhitlock
Автор

You are so brilliant !! Thanks for making these videos.

char