Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Add One Row To Tree
Please consume this content on for a richer experience. It is necessary to solve the questions while watching videos, enables that.
NADOS also enables doubt support, career opportunities and contests besides free of charge content for learning. In this question we print numbers in Increasing Order and Decreasing Order.
Question Name:
Add One Row To Tree
Question Link:
Question Statement:
Given the root of a binary tree and two integers val and depth, add a row of nodes with value val at the given depth depth.
Note that the root node is at depth 1.
The adding rule is:
Given the integer depth, for each not null tree node cur at the depth depth - 1, create two tree nodes with value val as cur's left subtree root and right subtree root.
cur's original left subtree should be the left subtree of the new left subtree root.
cur's original right subtree should be the right subtree of the new right subtree root.
If depth == 1 that means there is no depth depth - 1 at all, then create a tree node with value val as the new root of the whole original tree, and the original tree is the new root's left subtree.
Topic: #Add One Row To Tree #trees #binary Tree
#TimeComplexity: O(N)
#SpaceComplexity: O(N)
More Youtube PlayList:
Level 1 In Java English :
Level 1 In Java Hindi :
Level 2 In Java Hindi :
Constructive Algorithm - Level 3 :
Geometry - Level 3 :
Maths for CP - Level 3 :
DP - Level 3 :
Euler Tour - Level 3 :
Ternary Search :
Recursion & Backtracking - Level 1 :
Recursion & Backtracking - Level 2 :
Graphs - Level 1 :
Dyanmic Programming - Level 1 :
Generic Trees - Level 1 :
Time and Space - Level 1 :
Pepcoding has taken the initiative to provide counselling and learning resources to all curious, skillful and dedicated Indian coders. This video is part of the series to impart industry-level web development and programming skills in the community.
We also provide professional courses with live classes and placement opportunities.
For more free study resources and information about the courses, visit:
Have a look at our result:
Follow us on our Youtube page:
Follow us on our FB page:
Follow us on Instagram:
Follow us on LinkedIn:
Follow us on Pinterest:
Follow us on Twitter:
Happy Programming !!! Pep it up
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment