an HCL GUVI product

11. Valid BST Check

Required Input:

213

Expected Output:

True

Code In Python

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

Run Code?

Click Run Button to view compiled output

12. Mirror Tree

Required Input:

123

Expected Output:

Mirror created

Code In Python

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

Run Code?

Click Run Button to view compiled output

13. Lowest Common Ancestor

Required Input:

324

Expected Output:

3

Code In Python

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

Run Code?

Click Run Button to view compiled output

14. Zigzag Level Order Traversal

Required Input:

123

Expected Output:

[[1], [3, 2]]

Code In Python

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

Run Code?

Click Run Button to view compiled output

15. Right View of Binary Tree

Required Input:

123

Expected Output:

[1, 3]

Code In Python

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

Run Code?

Click Run Button to view compiled output

16. Left View of Binary Tree

Required Input:

123

Expected Output:

[1, 2]

Code In Python

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

Run Code?

Click Run Button to view compiled output

17. Root-to-Leaf Paths

Required Input:

123

Expected Output:

['1->2', '1->3']

Code In Python

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

Run Code?

Click Run Button to view compiled output

18. Vertical Order 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 vertical_order(root): # Write your logic here pass

Run Code?

Click Run Button to view compiled output

19. Build Tree from Inorder and Preorder

Required Input:

123
213

Expected Output:

Tree constructed

Code In Python

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

Run Code?

Click Run Button to view compiled output

20. Binary Tree to Doubly Linked List

Required Input:

213

Expected Output:

DLL created

Code In Python

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

Run Code?

Click Run Button to view compiled output

2 of 3