Lowest Common Ancestor of a Binary Search Tree - Leetcode 235 - Trees (Python)

  Рет қаралды 4,265

Greg Hogg

Greg Hogg

Күн бұрын

Пікірлер
@GregHogg
@GregHogg 5 ай бұрын
Master Data Structures & Algorithms For FREE at AlgoMap.io!
@kavan3
@kavan3 Ай бұрын
You could also just have the search function return a TreeNode which is the LCA we are looking for. So: def search(root: TreeNode, p: TreeNode, q: TreeNode) -> TreeNode: if not root: return None if p.val < root.val and q.val < root.val: return search(root.left, p, q) elif p.val > root.val and q.val > root.val: return search(root.right, p, q) else: return root
@SHIHJUIheh
@SHIHJUIheh 5 ай бұрын
This is a really good explanation. It's so much better than my professor's!
@supriya7720
@supriya7720 5 ай бұрын
Love your explanations. Please do more problems on LinkedLists, Sliding window, Two Pointers etc. I find your explanations much better than Neetcode🙈
@GregHogg
@GregHogg 5 ай бұрын
Glad to hear it! And I have playlists for all these categories :)
@niranjanbhat3949
@niranjanbhat3949 8 ай бұрын
Good explanation, i suggest u bring more dp and sliding window problems please 😊
@GregHogg
@GregHogg 8 ай бұрын
Thank you! I've got lots of sliding window, dp is coming up shortly
@user-jm6gp2qc8x
@user-jm6gp2qc8x 4 ай бұрын
Hey Greg, I think O(logN) is correct for time complexity.
@gaiusjuliuscaesar9296
@gaiusjuliuscaesar9296 7 ай бұрын
Title says problem 236 (LCA of a binary tree) but video is on problem 235 (LCA of a binary SEARCH tree)
@GregHogg
@GregHogg 7 ай бұрын
Thanks so much for noticing this!
@philipbrujic4728
@philipbrujic4728 5 ай бұрын
Another approach that would work is adding nonlocal lca in function scope
@ketkiambekar7607
@ketkiambekar7607 2 ай бұрын
Thanks for the simple explanation. I do have a question about why lca is a list and not a variable. Can you please point me to more explanation about lists being 'proper global variables'? Thanks
@prithvishdoshi1224
@prithvishdoshi1224 2 ай бұрын
The reason for using a list instead of a single value has to do with how Python handles mutable and immutable types. When you pass a list (which is mutable) to a function or reference it within a class, any changes made to the list (like appending or modifying its elements) will persist outside the function. This makes it act more like a "global" variable that can be updated from anywhere in the class. If you were to use lca = root (which is immutable), any attempt to modify var inside the method would create a new local variable, and var outside the method would remain unchanged.
Implement Trie (Prefix Tree) - Leetcode 208 - Trees (Python)
9:30
Diameter of Binary Tree - Leetcode 543 - Trees (Python)
11:16
Мясо вегана? 🧐 @Whatthefshow
01:01
История одного вокалиста
Рет қаралды 7 МЛН
Правильный подход к детям
00:18
Beatrise
Рет қаралды 11 МЛН
How to treat Acne💉
00:31
ISSEI / いっせい
Рет қаралды 108 МЛН
LOWEST COMMON ANCESTOR OF A BINARY TREE I | PYTHON | LEETCODE 236
12:48
Data science : Credit Card Fraud Detection using Machine Learning from Kaggle
46:35
Rotting Oranges - Leetcode 994 - Graphs (Python)
16:09
Greg Hogg
Рет қаралды 4 М.
Number of Islands - Leetcode 200 - Graphs (Python)
11:01
Greg Hogg
Рет қаралды 12 М.
Validate Binary Search Tree - Leetcode 98 - Trees (Python)
8:12
Tools EVERY Software Engineer Should Know
11:37
Tech With Tim
Рет қаралды 2,4 М.