No results found for the specified position. 14 JavaScript Intermediate Level Tree Interview Questions

MockQuestions

JavaScript Intermediate Level Tree Mock Interview

To help you prepare for your JavaScript Intermediate Level Tree interview, here are 14 interview questions and answer examples.

Get More Information About Our JavaScript Intermediate Level Tree Interview Questions

Question 1 of 14

Is the binary tree symmetric?

This interview question concentrates on using JavaScript recursive and iterative functions.

You are given a binary tree. You have to check if the tree is symmetric about its center.
A tree is symmetric when the left subtree and the right subtree of the root node are mirror images of each other as shown in the example below.

/*Example*/

            10
          /  |\
         15  | 15
        /  \ | / \
       1   2 | 2  1
The above tree is symmetric about its centre. So we will return true.

            10
          /  |\
         1   | 1
        / \  |  \
           2 |   2
The above tree is not symmetric about its centre. So we will return false.

Solution:

Approach I (Recursive):

For two nodes to be a mirror of each other, either they both should be non-null or both null. If only one of them is null, they are definitely not a mirror of each other.
If the nodes are non-null,
1. their value must be equal,
2. the left subtree of the first node must be the mirror of the right subtree of the second node,
3. the right subtree of the first node must be the mirror of the left subtree of the second node.

class Node {
    constructor(val, left, right) {
        this.val = val || 0
        this.left = left || null
        this.right = right || null
    }
}


/**
 * @param {Node} root
 * @return {boolean}
 */
function isTreeSymmetric(root) {
    if (!root) return true
    return isMirror(root.left, root.right)
}

function isMirror(node1, node2) {
    if (!node1 && !node2) return true
    if (!node1 || !node2) return false
    
    return(
        node1.val === node2.val &&
        isMirror(node1.left, node2.right) &&
        isMirror(node1.right, node2.left)
    )
}

Time complexity- O(number of nodes)
Space complexity- O(height of tree)

Approach II (Iterative):

The approach is almost the same as the recursive approach. We just use stack instead of recursion to keep track of nodes that we want to compare to check if they are the mirror of each other or not.
We maintain a stack array. First, we push two nodes left and right of the root node. Then in every iteration, we pop two nodes from the stack. Then we check the null-ness of both nodes. If they both are null, we continue two check other nodes in the stack.
If only one of them is null, we straightaway return false.
Then we compare their value. If values are equal, we push their children nodes in a specific order as with the recursive approach.

class Node {
    constructor(val, left, right) {
        this.val = val || 0
        this.left = left || null
        this.right = right || null
    }
}


/**
 * @param {Node} root
 * @return {boolean}
 */
function isTreeSymmetric(root) {
    if (!root) return true

    const stack = []
    stack.push(root.left)
    stack.push(root.right)
    
    while(stack.length !== 0) {
        const node1 = stack.pop()
        const node2 = stack.pop()
        
        if (!node1 && !node2) continue
        if (!node1 || !node2) return false
        if (node1.val != node2.val) return false
        
        stack.push(node1.left)
        stack.push(node2.right)

        stack.push(node1.right)
        stack.push(node2.left)
    }
    
    return true
}

Time complexity- O(number of nodes)
Space complexity- O(height of tree)

Written by on May 22nd, 2021

Next Question

14 JavaScript Intermediate Level Tree Interview Questions & Answers

Below is a list of our JavaScript Intermediate Level Tree interview questions. Click on any interview question to view our answer advice and answer examples. You may view 5 answer examples before our paywall loads. Afterwards, you'll be asked to upgrade to view the rest of our answers.

  • 1. Is the binary tree symmetric?

  • 2. Can you merge binary trees?

  • 3. How do you increase the skewed tree?

  • 4. Invert the binary tree.

  • 5. Are the two binary trees the same or not?

  • 6. What is the maximum depth of the binary tree?

  • 7. Using in-order traversal of the binary tree, return an array.

  • 8. What is the lowest common ancestor in a binary search tree?

  • 9. Can you find the zigzag level order traversal of the node values?

  • 10. How do you delete the key from the BST and return the modified tree root node?

  • 11. Return all elements in two binary search trees.

  • 12. Create a maximum binary tree.

  • 13. How do you prune the binary tree?

  • 14. Distribute coins in the binary tree.