Same Tree - Leetcode 100 (Python)

preview_player
Показать описание
leetcode, coding interview question, data structures, data structures and algorithms, faang
Рекомендации по теме
Комментарии
Автор

Master Data Structures & Algorithms and CRUSH your next Coding Interview for FREE at AlgoMap.io!

GregHogg
Автор

Bros vocal cords are running a non terminating voice crack loop

BlissOn
Автор

you do not need to check separate Null vs not null and value 1 vs value 2. If a != b should capture both cases

nagyzoli
Автор

This is true if you are checking that the trees have the same structure, but not if you have to check that they have the same values, as BSTs can have different structures with the same values

DavideCanton
Автор

Convert both to bytes, hash both, check and voilà !

lolous-studio
Автор

class Solution:
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
if p is None or q is None:
return p == q
return (p.val == q.val) and self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)

keyboardbasher
Автор

Would a BFS approach to this be better

alexgjeka
Автор

Just do preorder traversal of both and compare the results.

EternalCelestialChambers
Автор

I thought you wanted to do graph isomorphism on trees 😢

luckytrinh
Автор

Just to make a catchy acronym "fang", they leave out Microsoft but inlcude Netflix 🤣🤣

Samsung-zgql
Автор

second if statement can be a simple or, because the and has already been checked in the if statement above it 🤓☝️

axhraf
Автор

I wouldn’t copy a tree. Not the answer they want but if the tree is so critical that a copy of it would need to be tested to be 100% sure it wasn’t bad, I would just use the original. If we can’t use the original, then we designed something wrong.

airkami
Автор

I think the minimum requirement for this problem should be to not use recursion, otherwise this is nothing special to list comparison.

boyangli
Автор

Couldnt you simplify the 2nd if statement to if not p or not q? Since we already return above if both are null. Your 2nd if check is unnecessarily verbose.

bilalraja
Автор

Hi Greg. I wanted to ask as someone who is in last year of college but has never studied graphs or DP properly, Is your youtube playlist and algomap questions enough for these topics? I use Java by the way.

RTU-mwft
Автор

Wouldn't "if not q" return false if q is integer 0?

renzo
Автор

Easier: Its a duplicated binary tree so they should be the same. 😛

eyluismi
Автор

Let’s all hope that p and q are never 0

joelbacker
Автор

why can we not do else return False after #1?

NilAtabey
Автор

I seriously failed to understand the logic here

SabinSipai