Flip Equivalent Binary Trees - Leetcode 951 - Python

Поделиться
HTML-код
  • Опубликовано: 6 ноя 2024

Комментарии • 13

  • @NeetCode
    @NeetCode  3 года назад +2

    🌲 TREE PLAYLIST: ruclips.net/video/OnSn2XEQ4MY/видео.html

  • @jeremyliang7159
    @jeremyliang7159 3 года назад +6

    Beautiful explanation! I've always had trouble dealing with tree problems but your videos really help me a lot in understanding them!

  • @theendurance
    @theendurance 3 года назад +3

    your explanations are goated bro wish you had more vids. puts every other LC solution video to shame

  • @iuri0072
    @iuri0072 13 дней назад

    I solved by a different approach, in case you might find interesting... i did two dfs (one for each tree) and i stored the level and the parent of each node into a dict.
    on each pair (level, parent), i would check if the values add to each other (if any of the values are different, the sum would not be equal) - the only base case i had to consider is to add a buffer to differ in case 0 != empty becase in both cases, the sum of the level would be 0.
    here's the code, in case you're wondering:
    class Solution:
    def flipEquiv(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool:
    treeValues = defaultdict(int)
    def dfs(node, level, tree, parent):
    if not node: return
    treeValues[(level, parent)] += (node.val + 10) if tree == 1 else -(node.val + 10)
    dfs(node.left, level + 1, tree, node.val)
    dfs(node.right, level + 1, tree, node.val)

    dfs(root1, 0, 1, None)
    dfs(root2, 0, 2, None)
    for i in treeValues.items():
    if i[1] != 0: return False
    return True

  • @mrditkovich2339
    @mrditkovich2339 2 года назад +1

    Hi, @Neetcode. I would suggest to include the burning of a node problem in this tress playlist

  • @huanpinghuang1256
    @huanpinghuang1256 2 года назад

    Thank you so much for clear explanation

  • @humaneBicycle
    @humaneBicycle 5 месяцев назад

    what is the time complexity of this? isn't is exponential because we are calling 4 functions inside one and it calls another 4. so shouldn''t it be of the order O(4^n)?

  • @asdfgmnbvczxcv
    @asdfgmnbvczxcv 2 года назад +2

    7:36 Or r1 == r2 ;D

  • @hari8568
    @hari8568 9 месяцев назад

    Why can't we ise BFS and check in each level if the nodes are the same in each level using like an external set for each tree?

  • @boathecat919
    @boathecat919 2 года назад +1

    when you are comparing those 6 nodes, you are drawing faces.

  • @floriankubiak7313
    @floriankubiak7313 13 дней назад

    I chose a different approach to make it both long and complicated :)

  • @johnconnor7832
    @johnconnor7832 2 года назад

    neet god!

  • @jmarti1997jm
    @jmarti1997jm 2 года назад

    so smart