Create Binary Tree from Descriptions - Leetcode 2196 - Python

preview_player
Показать описание


0:00 - Read the problem
0:40 - Drawing Explanation
7:02 - Coding Explanation

leetcode 2196

#neetcode #leetcode #python
Рекомендации по теме
Комментарии
Автор

Never seen someone who can explain these types of problems as well as you. A very rare talent. Thank you!

WholeNewLevel
Автор

Yes, after two hard problem this one was a bit easy and therapeutic. Solved it on my own.

freecourseplatformenglish
Автор

all i wanted from my teachers, were simple explanations on how to solve this types of problems and you gave them. due to your help it has been even easy to convert the python code to cpp as per my necessity. thanks a lot

ashfaqmahmud
Автор

Thankyou bro yesterdays problem explanation helped me to implement today's problem successfully

lokesh
Автор

Man I really love to see a video uploaded by you this early and when I need it too!! Awesome work dude!!

arihantsinghrana
Автор

you can save creating children set by running through descriptions and creating children first, then initialise root variable to null and while passing through second time, only the root would return null when fetched from the nodes{}

mukulorama
Автор

man you really know what are you doing, thx bro . best youtuber ever `personals opinion` !!

mohanedomer
Автор

Optimization -
instead of making additional children set, we can make a dict "child_to_parent" mapping. init the root val to be random, And while loop root = child_to_parent[root] until none. You have a root with average case complexity of log n instead of O(n) everytime.

HimanshuTanwani
Автор

0:06 Yeah this was a nice chill one compared to yesterday.

fancypants
Автор

Haha you posted about something about inverting in linkedIn and here we are doing the same.

bishwashkumarsah
Автор

We needed this after yesterday to boost our confidence

pratyushthakur
Автор

Thank you so much for the daily leetcode

MP-nyep
Автор

I tried it by myself and i got almost correct answer.
And when I searched attach was completely different 😢

GeetainSaar
Автор

in fact you can check whether parent is in children in the main loop itself as we are iterating through the descriptions anyway. Saves a pass.

dasav
Автор

Hey, great work neetcode!! can you do me a favor and post the explanation video of #987. Struggling to solve it and there are barely any videos explaining the intuition, people are just writing the code and saying this is it!!

Thorffin
Автор

PLEASE SOLVE DAILY SIR, LOVE FROM INDIA.🤗

karthi
Автор

Java solution for those interested:

class Solution {
public TreeNode createBinaryTree(int[][] descriptions) {
int i = 0;

Map<Integer, TreeNode> map = new HashMap();
for (int[] d : descriptions)
map.put( d[1], new TreeNode(d[1]));

TreeNode root = null;
for (int[] d : descriptions) {
if ( !map.containsKey(d[0]))
{
root = new TreeNode(d[0]);
map.put(d[0], root);
}
if ( d[2] == 1 )
map.get(d[0]).left = map.get(d[1]);
else
map.get(d[0]).right = map.get(d[1]);
}

return root;
}
}

yang
Автор

my solution, same time complexity. maybe harder to read, tell me what you think:

childrens = set()
root = TreeNode()
parents = defaultdict(list)

for par, child, left in descriptions:
childrens.add(child)
parents[par].append([child, left])

for par, child, left in descriptions:
if par not in childrens:
root.val = par
break

def build(node):
if node.val not in parents:
return node

for child, left in parents[node.val]:
if left:
node.left = build(TreeNode(child))
else:
node.right = build(TreeNode(child))

return node

return build(root)

galkk
Автор

return (h:={v:TreeNode(v)for v in set(chain(*d))},
set(setattr(h[p], ['right', 'left'][i], h[c])for p, c, i in d),
h[sum(set(p for p, _, _ in d)-set(c for __, c, __ in d))]
)[-1]

qulinxao
visit shbcf.ru