"429. N-ary Tree Level Order Traversal", today's @LeetCode challenge explained with DFS and BFS methods.

A thread 🧵
#100daysOfCode
#leetcode
@LeetCode Problem Statement-
Given an n-ary tree, return the level order traversal of its nodes' values.

Nary-Tree input serialization is represented in their level order traversal, each group of children is separated by the null value (See examples). Image
@LeetCode Understanding the problem-
Usually, we're given tree problems which has left and right child but here is something different. For any node, it's child are given in form of node arrays. It means now any node can have more than 2 child also.
We've to find level order traversal.
@LeetCode Intuition-
At first sight, it looks a bit complex but when you observe, it's really simple and very similar to standard BST. We can simply apply standard BFS & DFS methods to solve this problem.

Let's see solutions.
@LeetCode Approach 1: BFS
-> Push current node in queue.
-> Traverse queue,
-> Pick front node from queue, add it's child to queue
and current node to answer array.
-> Repeat the process for its child.
Time Complexity: O(n)
Auxiliary Space: O(n) Image
@LeetCode Approach 2: DFS
-> Apply standard method, pass one extra parameter level
-> Add the current node to answer at it's level, call function for it's child.
Time Complexity: O(n)
Auxiliary Space: O(n) Image
@LeetCode Approach 2.1, Space Optimized
In the above idea, we used map to maintain level and then copied to answer. We can do this without map, by simply increasing size of answer when it becomes equal to level.
Time Complexity: O(n)
Auxiliary Space: O(n) Image
@LeetCode That's a wrap!

If you enjoyed this thread:

1. Follow me @Iampatelajeet for,
‍💻 Coding,
🌐 Case Studies,
👻 free ka gyaan,
🤖 tech stuffs and
😉 coding memes
2. RT is appreciated, share and let everyone be consistent. Start the challenge with me.

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Ajeet Patel || Leetcoder

Ajeet Patel || Leetcoder Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @Iampatelajeet

Sep 7
Sometimes the biggest problem while applying off-campus is that, we don't know which company to apply??

300+ Companies paying 12+ LPA for SDE-1 role in India, checkout the list below.

A thread 👇
Acko
Adobe
Airbase
Airbnb
Airbus
Airtel X Labs
Ajio
Akamai
AlphaGrep
Alphonso
Amagi
Amazon
American Express
Angel One
Ansys
Anthem
Apna
AppDynamics
Arcesium
Arista
Atlassian
AutoDesk
Auzmor
Avail Finance
Avalara
BCG
BNY Mellon
Bain & Company
Bankbazaar
Belzabar
Bharatpe
Bigbasket
Biofourmis
Bizongo
Blackbuck
Blinkit
BloomReach
Booking
Bright Money
Broadcom
Browserstack
Bukuwarung
Buyhatke
Byjus
C2FO
Cadence
CashFree
Chargebee
Chegg
Chronus Software
Cisco
Citi Bank
Citrix
Citymall
Read 15 tweets
Sep 7
Construct String from Binary Tree, today's @LeetCode challenge explained in recursive and iterative method.

A thread 🧵
#100daysOfCode
Problem Statement -
Given the root of a binary tree, construct a string consisting of parenthesis and integers from a binary tree with the preorder traversal way, and return it.
Omit all the empty parenthesis pairs that do not affect the one-to-one mapping relationship between the string and the original binary tree.
Read 10 tweets
Sep 6
If you are inconsistent in practice, start 100 days of challenge and mention me I'll support you. Let's see today's solution,

0ms, faster than 100%, recursive approach of today's @LeetCode challenge.

A thread 🧵
#100daysOfCode
Problem Statement-
Given the root of a binary tree, return the same tree where every subtree (of the given tree) not containing a 1 has been removed.

A subtree of a node node is node plus every node that is a descendant of node. Image
Understanding the problem-
Basically, we need to remove every subtree which doesn't have node of value 1.
Read 7 tweets
Jul 30
It's better to solve one problem everyday instead of solving 10 once in a week.
~Consistency

In the series of solving daily @LeetCode challenges, let's see today's really interesting problem.

#100daysOfCode
@LeetCode Problem statement:

You are given two string arrays words1 and words2.

A string b is a subset of string a if every letter in b occurs in a including multiplicity.

For example, "wrr" is a subset of "warrior" but is not a subset of "world".
@LeetCode A string a from words1 is universal if for every string b in words2, b is a subset of a.

Return an array of all the universal strings in words1. You may return the answer in any order.

Example 1:
Read 13 tweets
Jul 29
"If you wanted to be rich in the 1800s you did it with Labour.
In the 1900s with Capital.
Now you do it with code."

@naval

In the series of solving @Leetcode daily challenges, we've an awesome problem today, 2 more days to finish a month, let's see how we can solve it.
@naval @LeetCode Find and Replace Pattern:
Input: words = ["abc","deq","mee","aqq","dkd","ccc"], pattern = "abb"
Output: ["mee","aqq"] problem statement
@naval @LeetCode Well, coming to solution part:

Approach 1:
-> we're given a set of strings, let's do it for one string to compare with given pattern.
-> For any string to a pattern of given 'pattern' string,
-> Both length must be equal
-> The number of unique characters must be equal approach 1 code.
Read 7 tweets
Jul 28
I was going through the threads of @Julian, I found some on startups, writing, career, storytelling, audience building, mental models, thinking and more which are not less than a goldmine, if you are eager to learn.

I'm gonna read them one by one, you shouldn't miss this. 🧵
@Julian -> Another perspective of seeing the world, it's not how you think:
Read 10 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(