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
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.
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
@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.
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)
they are two separate nodes held in two separate locations in memory, comparing the nodes by "reference" (their memory location) they are not the same, but using comparison by "value" they are equal.
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
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
if not left or not right or left.val != right.val:
return False
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.
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
After a long gap
Neet code is back
@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.
Thanks!
bro really have good amount of money
Bhai thoda mujhe bhi de de
Very well explained thank you so much👏
Heck yeah, thanks for the vid! ^^
was expecting you daily bro, please try to cover the past daily challenge problems, kind request🥺🥺🥺🥺🥺
Check if left subtree is equal to inverted right subtree. Done.
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)
Welcome back
Yo! How was your India trip? Did you have good food?
Thanks for the video.
Yeah, it was a lot of fun! But was thinking about work most of the time 😅
@@NeetCodeIO was manager calling you??
Hello, can someone tell me what Aplication is he using for draw?
Is that a website or app?
a web extension
@@BurhanAijaz which one?
it's Paint 3D
how dfs function accepts only one argument at the time of returning to the isSymetric function
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 )😅😅
why did you return dfs()?
the method needs to return the boolean value
❤❤❤❤❤
I traverse the tree and check the returning array was a palindrome
195th testcase in leetcode didnt pass. Expected is False but getting True. Tree is [1,2,2,2,null,2] for ref..
why have to use val in line 15?
they are two separate nodes held in two separate locations in memory, comparing the nodes by "reference" (their memory location) they are not the same, but using comparison by "value" they are equal.
Is that easy? Ffs
Первонахи жрут какахи
У меня такая задача была в Тинькове на собесе, оказывается она и тут есть.
@@Hello-g5b3p на какую должность?
Thanks!
Welcome back
Thanks!
Thanks!
Kuch Mujhe bhee dede
@@pkyadav6230lmaoo
bro stole his mom's credit card
Thanks!
Thanks!