Symmetric Tree - Leetcode 101 - Python

preview_player
Показать описание

Solving Leetcode 101 - Symmetric Tree, today's daily leetcode problem on March 12th.

0:00 - Read the problem
0:30 - Drawing Explanation
2:55 - Coding Explanation

leetcode 101

#leetcode #neetcode #python
Рекомендации по теме
Комментарии
Автор

Also iterative BFS version in Python in case anyone needs

from collections import deque
class Solution:
def isSymmetric(self, root: Optional[TreeNode]) -> bool:
q = deque([(root, root)])
while q:
left, right = q.popleft()
if not left and not right:
continue
if not left or not right:
return False
if left.val != right.val:
return False
q.append((left.left, right.right))
q.append((left.right, right.left))
return True

Endericos
Автор

Null handling (at least in Java) for left and right nodes can be done a bit easier: if one of them is null, all we need to do is check for their equality with == operator:

if (left == null || right == null) {
return left == right;
}

Not sure if it works in python, though.

keeprocking
Автор

I love how you posted this the exact same day i gave up on it when i thought .
Keep going bruh you're doing awesome work <3

eshabaweja
Автор

It's always good to come here even when I don't find difficulties while solving the problem. I compare left.left.value and right.right.value, didn't think of comparing itself. Thanks for uploading great videos. You had me enjoy solving problems

licokr
Автор

@NeetcodeIO do you happen to have a published leetcode list for Neetcode All List? I finished the Neetcode 150 and would like to test on all of the Neetcode All questions I had not seen. Thank you for all your work and great teaching.

infiniteloop
Автор

Check if left subtree is equal to inverted right subtree. Done.

energy-tunes
Автор

Very well explained thank you so much👏

bhabishyachaudhary
Автор

Can you invert one sub tree and see if now both sub trees are identical? I did so and even though both sub tress are the same but doesn't look like I can just do a direct comparison of them.
Something like this:
def isSymmetric(root):

def invertTree(root):
if not root:
return None

root.left, root.right = root.right, root.left
invertTree(root.left)
invertTree(root.right)

return root

return root.left == invertTree(root.right)

linainsworth
Автор

was expecting you daily bro, please try to cover the past daily challenge problems, kind request🥺🥺🥺🥺🥺

tg-shyam
Автор

Yo! How was your India trip? Did you have good food?

Thanks for the video.

saurabhd
Автор

Hello, can someone tell me what Aplication is he using for draw?
Is that a website or app?

elvisgarcia
Автор

how dfs function accepts only one argument at the time of returning to the isSymetric function

ABINETDEGEFA
Автор

I came here to see if he had done the bfs solution. Can someone explain the bfs solution. Its kinda not straight forward (for me at least )😅😅

perelium-x
Автор

I traverse the tree and check the returning array was a palindrome

cubingmachine