an HCL GUVI product

Binary Trees - DSA Interview Questions

Binary Trees are a fundamental data structure used for efficient searching, sorting, and hierarchical data representation. Understanding tree traversal techniques, recursion, and tree properties is essential for solving complex problems in graph theory, database indexing, and AI-based decision trees. Mastering binary trees helps in solving tree-based interview questions efficiently.

Practice Binary Trees DSA Coding Problems with Solutions

Learning Objectives:

Learn tree traversal techniques (Inorder, Preorder, Postorder, Level Order) and their applications. Understand recursive and iterative approaches to solving tree problems effectively.

Exercise Instructions:

  • Start with the first problem and attempt to solve it before checking the hint or solution.
  • Ensure you understand the logic behind each solution, as this will help you with more complex problems.
  • Use these exercises to reinforce your learning and identify areas that may require further study.

1. Preorder Traversal

Required Input:

123

Expected Output:

[1, 2, 3]

Code In Python

class TreeNode: def __init__(self, val): self.val = val self.left = None self.right = None def preorder_traversal(root): # Write your logic here pass

Run Code?

Click Run Button to view compiled output

2. Inorder Traversal

Required Input:

123

Expected Output:

[2, 1, 3]

Code In Python

class TreeNode: def __init__(self, val): self.val = val self.left = None self.right = None def inorder_traversal(root): # Write your logic here pass

Run Code?

Click Run Button to view compiled output

3. Postorder Traversal

Required Input:

123

Expected Output:

[2, 3, 1]

Code In Python

class TreeNode: def __init__(self, val): self.val = val self.left = None self.right = None def postorder_traversal(root): # Write your logic here pass

Run Code?

Click Run Button to view compiled output

4. Level Order Traversal

Required Input:

123

Expected Output:

[[1], [2, 3]]

Code In Python

class TreeNode: def __init__(self, val): self.val = val self.left = None self.right = None def level_order(root): # Write your logic here pass

Run Code?

Click Run Button to view compiled output

5. Height of Binary Tree

Required Input:

123

Expected Output:

2

Code In Python

class TreeNode: def __init__(self, val): self.val = val self.left = None self.right = None def tree_height(root): # Write your logic here pass

Run Code?

Click Run Button to view compiled output

6. Maximum Element in Binary Tree

Required Input:

123

Expected Output:

3

Code In Python

class TreeNode: def __init__(self, val): self.val = val self.left = None self.right = None def max_element(root): # Write your logic here pass

Run Code?

Click Run Button to view compiled output

7. Symmetric Tree Check

Required Input:

123

Expected Output:

True

Code In Python

class TreeNode: def __init__(self, val): self.val = val self.left = None self.right = None def is_symmetric(root): # Write your logic here pass

Run Code?

Click Run Button to view compiled output

8. Identical Trees Check

Required Input:

1 1

Expected Output:

True

Code In Python

class TreeNode: def __init__(self, val): self.val = val self.left = None self.right = None def are_identical(root1, root2): # Write your logic here pass

Run Code?

Click Run Button to view compiled output

9. Diameter of Binary Tree

Required Input:

123

Expected Output:

2

Code In Python

class TreeNode: def __init__(self, val): self.val = val self.left = None self.right = None def diameter(root): # Write your logic here pass

Run Code?

Click Run Button to view compiled output

10. Sum of All Nodes

Required Input:

123

Expected Output:

6

Code In Python

class TreeNode: def __init__(self, val): self.val = val self.left = None self.right = None def sum_of_nodes(root): # Write your logic here pass

Run Code?

Click Run Button to view compiled output

1 of 3