623. Add One Row to Tree

623. Add One Row to Tree

Problem Solving - Day 5

ยท

4 min read

Hello, reader ๐Ÿ‘‹๐Ÿฝ ! Welcome to day 5 of the series on Problem Solving. Through this series, I aim to pick up at least one question everyday and share my approach for solving it.

Today, I will be picking up LeetCode's daily challenge problem. Add One Row to Tree. This was a classic question to practice BFS and DFS traversal in a tree.


๐Ÿค” Problem Statement

Given the root of a tree, value and depth, insert new nodes with given value to the right and left sub tree of the node at depth-1. It is also given that the depth of the root is 1.

  • If the depth given is 1, then the new node with value should be the root node, and the original root should be in the left subtree of the newly created node.
  • The left subtree of the node where the insertion happens should become the left subtree of the new node.
  • Similarly, the right subtree of the node where the insertion happens should become the right subtree of the new node.
  • E.g.: Input: [4,2,6,3,1,5], val: 1, depth: 2
    • Output: [4,1,1,2,null,null,6,3,1,5]

Constraint: 1 <= depth <= the depth of tree + 1
Link to the question


๐Ÿ’ฌ Thought Process - DFS Traversal

  • From the constraints given, we know that there will always be at least one node in the tree and also the depth will be at least 1 and at most equal to the depth of tree + 1. So we don't have to handle these edge cases. Whew!
  • From the question, it's very clear that we need to attach the node at depth such that the node at depth-1 is the parent of newly inserted nodes.
  • But if we travel until the given depth, we won't be able to form the link between new nodes and the node at depth-1.
  • So, instead of traversing until the given depth, we will traverse until the previous node, i.e., depth-1, and then change the link of the current node to the new nodes.
  • This way we will also be able to link the previous left and right subtree to the new nodes.

insert_nodes_at_depth.png

  • I will be using DFS (using recursion) to solve this problem.

๐Ÿ‘ฉ๐Ÿฝโ€๐Ÿ’ป Solution - DFS Traversal

  • Below is the code for the approach discussed above:

    /**
    * Definition for a binary tree node.
    * public class TreeNode {
    *     int val;
    *     TreeNode left;
    *     TreeNode right;
    *     TreeNode() {}
    *     TreeNode(int val) { this.val = val; }
    *     TreeNode(int val, TreeNode left, TreeNode right) {
    *         this.val = val;
    *         this.left = left;
    *         this.right = right;
    *     }
    * }
    */
    class Solution {
      public TreeNode addOneRow(TreeNode root, int val, int depth) {
          // If no node found
          if(root == null) {
              return new TreeNode(val);
          }
    
          // Insert at root as there's no depth = 0.
          // Make the left subtree of the new root as previous root
          if(depth == 1) {
              return new TreeNode(val, root, null);
          }
    
          return addOneRowHelper(root, val, depth-1);
      }
    
      private TreeNode addOneRowHelper(TreeNode root, int val, int depth) {
          // If the root is null, just return null
          if(root == null) {
              return null;
          }
    
          // If you are at depth-1, then this node is the parent of new nodes
          if(depth-1 == 0) {
              TreeNode leftNode = new TreeNode(val, root.left, null);
              TreeNode rightNode = new TreeNode(val, null, root.right);
    
              root.left = leftNode;
              root.right = rightNode; 
          }
          // If not, then traverse left sub tree and right subtree until depth-1 == 0
          else {
              root.left = addOneRowHelper(root.left, val, depth-1);
              root.right = addOneRowHelper(root.right, val, depth-1);
          }
    
          return root;
      }
    }
    
    Time Complexity: O(n)
      n => number of nodes
    Space Complexity: Implicit O(n) stack space
      n => depth of tree (in the worst case if tree is skewed, then the depth = n)
    


  • Although the above problem was solved using DFS and recursion, it can also be solved using DFS with stack data structure as well as using BFS with Queue data structure.
  • For the approach using DFS with stack data structure, the time and space complexity remain same => O(n), n = number of nodes
  • But, for the approach using BFS with queue data structure, the time complexity: O(n) but then since the queue would hold all the nodes at a level, at max there could only be x nodes. space complexity: O(x) x = maximum number of nodes at any level and x<n.

  • I am not going to be explaining the other two approaches since they are straightforward BFS and DFS. But you can find the code here.


Conclusion

That's a wrap for todays problem. If you liked my explanation then please do drop a like/ comment. Also, please correct me if I've made any mistakes or if you want me to improve something!

Thank you for reading!