Binary Tree Inorder Traversal, today's @Leetcode challenge: Understanding the problem to intuition to solution, 3 approaches.

A thread 🧵
#100daysOfCode #leetcode
Problem Statement-
Given the root of a binary tree, return the inorder traversal of its nodes' values.
Understanding the problem-
It's a really standard problem: one of the DFS traversals.
Intuition-
In this traversal, for any subtree, we first choose left node, root, then right node. Let's see the approaches.
Solution: Approach 1- Recursion
Time Complexity: O(n)
Space: O(n)
Approach 2: Iterative
-> Uses stack to store the nodes.
-> Pick the root node
-> go to left most node, push all them in stack
-> Move to right subtree.
Time Complexity: O(n)
Space: O(n)
Approach 3: Morris Traversal
Basically it's the process of making the tree, skewed.
-> Pick the root node
-> Add it to the left most node at every step.
Time Complexity: O(n)
Space: O(1)
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 practice 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.
Understanding the problem-
Basically, we need to remove every subtree which doesn't have node of value 1.
Read 7 tweets
Sep 5
"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.
Read 8 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

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!

:(