No results found for the specified position. 8 Python Intermediate Level Tree Interview Questions

MockQuestions

Python Intermediate Level Tree Mock Interview

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

Get More Information About Our Python Intermediate Level Tree Interview Questions

Question 1 of 8

Is the binary tree symmetric?

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

You are given a binary tree. You have to check if the tree is symmetric about its centre.
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 center. 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,
their value must be equal,
the left subtree of the first node must be the mirror of the right subtree of the second node,
the right subtree of the first node must be the mirror of the left subtree of the second node.

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
 
def isMirror(root1, root2):
	‘’’ 
returns True if both root1 and root2 are mirrors of each other 
‘’’
	if not root1 and not root2:
		return True
	
	if not root1 or not root2:
		return False
	
	if root1.val != root2.val:
		return False
	
	isLeftMirror = isMirror(root1.left, root2.right)
	isRightMirror = isMirror(root1.right, root2.left)
	
	return isLeftMirror and isRightMirror
 
 
def isTreeSymmetric(root):
	‘’’ 
Input: (TreeNode) root of tree
Output: (TreeNode) root of reversed tree  
‘’’
	if not root:
		return True
	return isMirror(root.left, root.right)

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 mirror each other.

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 straight away 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 TreeNode:
    def __init__(self, val = 0, left = None, right = None):
        self.val = val
        self.left = left
        self.right = right
 
def isTreeSymmetric(root):
	if not root:
		return True
 
	stack = []
	stack.append(root.left)
	stack.append(root.right)
	
	while stack:
		node1 = stack.pop()
		node2 = stack.pop()
		
		if not node1 and not node2:
			continue
			
		if not node1 or not node2:
			return False
		
		if node1.val != node2.val:
			return False
		
		stack.append(node1.left)
		stack.append(node2.right)
		
		stack.append(node1.right)
		stack.append(node2.left)
	
	return True

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

Written by on June 27th, 2021

Next Question

8 Python Intermediate Level Tree Interview Questions & Answers

Below is a list of our Python 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?