Uploaded May 2019 | Updated November 2026, 3 hours ago
Try Our Full Platform: nas.io/backtobackswe
πΉ Intuitive Video Explanations
βNew Unseen Questions
π Get All Solutions
Question: Given a root node for a binary tree, serialize the binary tree into a string representation. Deserialize that same string representation into a tree identical to the original tree given.
The key is to understand what our fundamental operation/job is in each call to the function.
Our fundamental job is to encode/materialize a single node/int value.
Compiler Design Phases: geeksforgeeks.org/compiler-design-phases-compiler
++++++++++++++++++++++++++++++++++++++++++++++++++
HackerRank: youtube.com/channel/UCOf7UPMHBjAavgD0Qw5q5ww
Tuschar Roy: youtube.com/user/tusharroy2525
GeeksForGeeks: youtube.com/channel/UC0RhatS1pyxInC00YKjjBqQ
Jarvis Johnson: youtube.com/user/VSympathyV
Success In Tech: youtube.com/channel/UC-vYrOAmtrx9sBzJAf3x_xw
++++++++++++++++++++++++++++++++++++++++++++++++++
Elements of Programming Interviews problem 10.13
Try Our Full Platform: nas.io/backtobackswe
πΉ Intuitive Video Explanations
βNew Unseen Questions
π Get All Solutions
Question: Given a root node for a binary tree, serialize the binary tree into a string representation. Deserialize that same string representation into a tree identical to the original tree given.
The key is to understand what our fundamental operation/job is in each call to the function.
Our fundamental job is to encode/materialize a single node/int value.
Compiler Design Phases: geeksforgeeks.org/compiler-design-phases-compiler
++++++++++++++++++++++++++++++++++++++++++++++++++
HackerRank: youtube.com/channel/UCOf7UPMHBjAavgD0Qw5q5ww
Tuschar Roy: youtube.com/user/tusharroy2525
GeeksForGeeks: youtube.com/channel/UC0RhatS1pyxInC00YKjjBqQ
Jarvis Johnson: youtube.com/user/VSympathyV
Success In Tech: youtube.com/channel/UC-vYrOAmtrx9sBzJAf3x_xw
++++++++++++++++++++++++++++++++++++++++++++++++++
Elements of Programming Interviews problem 10.13
![Generate All Strings With n Matched Parentheses - Backtracking (Generate Parentheses on LeetCode)
Free 5-Day Mini-Course: https://backtobackswe.com
Try Our Full Platform: https://backtobackswe.com/pricing
πΉ Intuitive Video Explanations
π Run Code As You Learn
πΎ Save Progress
βNew Unseen Questions
π Get All Solutions
Question: Write a program that takes as input a number n in and returns all the strings with n matched pairs of parens.
Examples:
n = 1
[ () ]
n = 2
[ (()), ()() ]
n = 3
[ ((())),(()()),(())(),()(()),()()() ]
Approach 1 (Brute Force Then Validate)
Generate all (2 ^ (2n)) possible parenthese strings and then validate each for being balanced.
If n = 2 then the string length will be 2 times that since all open parentheses are matched by closed parentheses.
This lower bounds our time complexity.
Even if we restrict the enumeration to just sets with an equal number of left and right parentheses we will have choose(2k, k) strings to consider for validation.
Approach 2 (Directed Backtracking)
The 3 Keys To Backtracking
Our Choice:
Whether we place a left or right paren at a certain decision point in our recursion.
Our Constraints:
We cant place a right paren unless we have left parens to match against.
Our Goal:
Place all k left and all k right parens.
The Key
At each point of constructing the string of length 2k we make a choice.
We can place a ( and recurse or we can place a ) and recurse.
But we cant just do that placement, we need 2 critical pieces of information.
The amount of left parens left to place.
The amount of right parens left to place.
We have 2 critical rules at each placement step.
We can place a left parentheses if we have more than 0 left to place.
We can only place a right parentheses if there are left parentheses that we can match against.
We know this is the case when we have less left parentheses to place than right parentheses to place.
Once we establish these constraints on our branching we know that when we have 0 of both parens to place that we are done, we have an answer in our base case.
++++++++++++++++++++++++++++++++++++++++++++++++++
HackerRank: https://www.youtube.com/channel/UCOf7UPMHBjAavgD0Qw5q5ww
Tuschar Roy: https://www.youtube.com/user/tusharroy2525
GeeksForGeeks: https://www.youtube.com/channel/UC0RhatS1pyxInC00YKjjBqQ
Jarvis Johnson: https://www.youtube.com/user/VSympathyV
Success In Tech: https://www.youtube.com/channel/UC-vYrOAmtrx9sBzJAf3x_xw
++++++++++++++++++++++++++++++++++++++++++++++++++
This question is number 16.6 in Elements of Programming Interviews by Adnan Aziz, Tsung-Hsien Lee, and Amit Prakash. Generate All Strings With n Matched Parentheses - Backtracking (Generate Parentheses on LeetCode)](https://i.ytimg.com/vi/sz1qaKt0KGQ/mqdefault.jpg)


![Increment An Integer Represented As An Array (Plus One on LeetCode)
Free 5-Day Mini-Course: https://backtobackswe.com
Try Our Full Platform: https://backtobackswe.com/pricing
πΉ Intuitive Video Explanations
π Run Code As You Learn
πΎ Save Progress
βNew Unseen Questions
π Get All Solutions
Question: Given an array that represents an integer digit by digit, perform an increment by 1 operation on it.
Examples:
1
Input
[ 1, 2, 9 ]
Output
[ 1, 3, 0 ]
2
Input
[ 9, 9, 9, 9 ]
Output
[ 1, 0, 0, 0, 0 ]
Approach 1 (Brute Force)
Convert the array to an integer.
Increment the integer.
Re-codify the integer as an array or just place it back into the original array digit by digit.
When we have an array of a certain size this will fail because of integer overflow on the initial conversion (array to integer).
Whenever we get an integer represented as an array or an integer as a string we are never going to go back to its original form.
The point of the question is for you to do the operation within the confines of the question.
Approach 2 (Increment Within The Array)
We can just simulate the incrementing by 1 starting at the last element.
If we increment and that element becomes 10 we need to continue incrementing to the left until we reach the start of the array.
Edge Case: We then check the first element, if it is 10 then we need to expand the array by 1 and set the first element to 1.
Complexities
Time: O( n )
We may potentially have to perform n increments followed by lengthening the array to add a 1 in the most significant digit
All of these operations run in linear time.
Space: O( 1 )
We operate within the original array given and do not create additional space.
In the case where we need to expand the array by 1 element at the front we will be creating a new array and copy the elements over but this is an edge case and is an internal API operation so isnt often counted.
++++++++++++++++++++++++++++++++++++++++++++++++++
HackerRank: https://www.youtube.com/channel/UCOf7UPMHBjAavgD0Qw5q5ww
Tuschar Roy: https://www.youtube.com/user/tusharroy2525
GeeksForGeeks: https://www.youtube.com/channel/UC0RhatS1pyxInC00YKjjBqQ
Jarvis Johnson: https://www.youtube.com/user/VSympathyV
Success In Tech: https://www.youtube.com/channel/UC-vYrOAmtrx9sBzJAf3x_xw
++++++++++++++++++++++++++++++++++++++++++++++++++
This question is number 6.2 in Elements of Programming Interviews by Adnan Aziz, Tsung-Hsien Lee, and Amit Prakash. Increment An Integer Represented As An Array (Plus One on LeetCode)](https://i.ytimg.com/vi/vA0t42qwKO0/mqdefault.jpg)






