Symmetric Tree - Leetcode 101 - Python

Поділитися
Вставка
  • Опубліковано 6 січ 2025

КОМЕНТАРІ • 46

  • @eshabaweja
    @eshabaweja Рік тому +5

    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

  • @Endericos
    @Endericos 8 місяців тому +15

    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

    • @xendu-d9v
      @xendu-d9v Місяць тому

      if not left or not right or left.val != right.val:
      return False

  • @keeprocking3620
    @keeprocking3620 10 місяців тому +2

    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.

  • @licokr
    @licokr 10 місяців тому

    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

  • @satwiktatikonda764
    @satwiktatikonda764 Рік тому +4

    After a long gap
    Neet code is back

  • @infiniteloop5449
    @infiniteloop5449 Рік тому +4

    @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.

  • @yasarapudurgabhavani3985
    @yasarapudurgabhavani3985 Рік тому

    Thanks!

  • @bhabishyachaudhary3495
    @bhabishyachaudhary3495 Рік тому

    Very well explained thank you so much👏

  • @Vancha112
    @Vancha112 Рік тому

    Heck yeah, thanks for the vid! ^^

  • @tg-shyam2887
    @tg-shyam2887 Рік тому +1

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

  • @energy-tunes
    @energy-tunes 7 місяців тому

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

  • @linainsworth3566
    @linainsworth3566 3 місяці тому

    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)

  • @yang5843
    @yang5843 Рік тому

    Welcome back

  • @saurabhd7990
    @saurabhd7990 Рік тому +2

    Yo! How was your India trip? Did you have good food?
    Thanks for the video.

    • @NeetCodeIO
      @NeetCodeIO  Рік тому +8

      Yeah, it was a lot of fun! But was thinking about work most of the time 😅

    • @sharathkumar8338
      @sharathkumar8338 Рік тому +1

      @@NeetCodeIO was manager calling you??

  • @elvisgarcia1822
    @elvisgarcia1822 Рік тому +3

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

  • @ABINETDEGEFA
    @ABINETDEGEFA 9 місяців тому

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

  • @perelium-x
    @perelium-x 9 місяців тому

    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 )😅😅

  • @seandean2805
    @seandean2805 11 місяців тому

    why did you return dfs()?

    • @CHIRANJIBNANDY1
      @CHIRANJIBNANDY1 6 місяців тому

      the method needs to return the boolean value

  • @chandankhuntia7017
    @chandankhuntia7017 Рік тому

    ❤❤❤❤❤

  • @cubingmachine1968
    @cubingmachine1968 Рік тому +1

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

    • @thevinarokiarajl2149
      @thevinarokiarajl2149 Рік тому

      195th testcase in leetcode didnt pass. Expected is False but getting True. Tree is [1,2,2,2,null,2] for ref..

  • @pedrohenriqueleite9382
    @pedrohenriqueleite9382 Рік тому

    why have to use val in line 15?

    • @kkp2174
      @kkp2174 Рік тому

      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.

  • @Jack-ss4re
    @Jack-ss4re Рік тому +1

    Is that easy? Ffs

  • @SadgeZoomer
    @SadgeZoomer Рік тому

    Первонахи жрут какахи

    • @Hello-g5b3p
      @Hello-g5b3p Рік тому

      У меня такая задача была в Тинькове на собесе, оказывается она и тут есть.

    • @GorgeousPuree
      @GorgeousPuree 4 місяці тому

      @@Hello-g5b3p на какую должность?

  • @yasarapudurgabhavani3985
    @yasarapudurgabhavani3985 Рік тому

    Thanks!

  • @krateskim4169
    @krateskim4169 Рік тому

    Welcome back

  • @yasarapudurgabhavani3985
    @yasarapudurgabhavani3985 Рік тому

    Thanks!

  • @yasarapudurgabhavani3985
    @yasarapudurgabhavani3985 Рік тому

    Thanks!

  • @yasarapudurgabhavani3985
    @yasarapudurgabhavani3985 Рік тому +10

    Thanks!

  • @yasarapudurgabhavani3985
    @yasarapudurgabhavani3985 Рік тому +9

    Thanks!