Tree: Add One Row to Tree

Tree: Add One Row to Tree

Day 17:

Problem 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:

root node: depth 1. The adding rule is:

  • Given : currDepth == 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 and cur's original right subtree should be the right subtree of the new right subtree root.
  • If depth == 1, 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.

Input: root = [4,2,6,3,1,5], val = 1, depth = 2
Output: [4,1,1,2,null,null,6,3,1,5]
        


Approach : DFS

  1. If depth =1, then create new RootNode and assign original root as left child of new root.
  2. Maintain curr variable,to keep track of currentDepth of node.
  3. Now recursively call left and right subtree with increasing current ,till curr is equal to depth -1.
  4. If curr ==depth-1 , then create two tempNode (leftTemp and rightTemp) and store current left and right respectively.
  5. Update currLeft and currRight with new val.
  6. Assign tempNode to left of currentleft and right of currentRight.

Code:

  • Time Complexity: The time complexity of this approach is O(N), where N is the number of nodes in the binary tree, as we need to traverse the entire tree to add the new row.
  • Space Complexity: The space complexity is also O(N), where N is the number of nodes, due to the recursive calls and the stack space used during traversal.


要查看或添加评论,请登录

Suyash Awathe的更多文章

  • Monotonic Array / TwoPointer :Trapping Rain Water: Most Asked !!

    Monotonic Array / TwoPointer :Trapping Rain Water: Most Asked !!

    Day 16/75 : Problem : Trapping Rain Water -(Hard) Given n non-negative integers representing an elevation map where the…

  • Stack: April POTD

    Stack: April POTD

    Question: Remove K Digits -(Medium) Given string num representing a non-negative integer num, and an integer k, return…

  • Queue-Array: April POTD

    Queue-Array: April POTD

    Day 14/75: Question: Reveal Cards In Increasing Order -(Medium) You are given an integer array . There is a deck of…

    1 条评论
  • Array: 9 April POTD

    Array: 9 April POTD

    Day 13/75: Question: Time Needed to Buy Tickets -(Easy) There are people in a line queuing to buy tickets, where the…

  • Queue : April POTD

    Queue : April POTD

    Day 12/75: Question: Number of Students Unable to Eat Lunch -(Easy) The school cafeteria offers circular and square…

    1 条评论
  • String : Parentheses : April-POTD

    String : Parentheses : April-POTD

    Day 11/75: Question: Minimum Remove to Make Valid Parentheses -(Medium) Given a string s of , and lowercase English…

    3 条评论
  • String : 5April POTD

    String : 5April POTD

    Day 10/75: Question: Make The String Great -(Easy) Given a string of lower and upper case English letters. A good…

  • String Parentheses : 4April POTD

    String Parentheses : 4April POTD

    Day 9/75: Question: Maximum Nesting Depth of the Parentheses -(Easy) A string is a valid parentheses string (denoted…

  • BackTracking : WordSearch :4April POTD

    BackTracking : WordSearch :4April POTD

    Day 9/75: Question: Word Search -(Medium) Given an m x n grid of characters board and a string word, return true if…

    1 条评论
  • Isomorphic String : April POTD

    Isomorphic String : April POTD

    Day 8/75: Question: Given two strings s and t, determine if they are isomorphic. Two strings and are isomorphic if the…

    2 条评论

社区洞察

其他会员也浏览了