Professional Documents
Culture Documents
Winsem2023-24 Bsts302p Ss Ch2023240500208 Reference Material I 09-02-2024 Boundary Traversal
Winsem2023-24 Bsts302p Ss Ch2023240500208 Reference Material I 09-02-2024 Boundary Traversal
TRAVERSAL
Boundary Traversal Method
EXPLANATION
structure. In the context of binary trees, which are a common type of tree
a specific order: the left boundary, the leaf nodes, and the right boundary.
Boundary Traversal Method
The order of these steps ensures that each node is visited exactly once, and
the traversal covers the entire boundary of the binary tree
Boundary Traversal Method
Example: 1
/ \
2 3
/ \ / \
4 5 6 7
Boundary Traversal:
Complete Order: 1 → 2 → 4 → 5 → 6 → 7 → 3
Boundary Traversal Method
Example: 10
/ \
2 13
\ / \
8 12 15
/ \
4 9
Boundary Traversal:
Complete Order: 10 → 2 → 8 → 4 → 9 → 12 → 15 → 13
Boundary Traversal Method
Example: 8
/ \
3 10
/ \ \
1 6 14
/ \ /
4 7 13
Boundary Traversal:
Complete Order: 8 → 3 → 1 → 6 → 4 → 7 → 13 → 14 → 10
Boundary Traversal Method
Algorithm:
1. Input:
2. Output:
direction.
Boundary Traversal Method
Algorithm:
3. Procedure:
2. Call `printLeftBoundary` with the left child of the root, excluding the
leftmost leaf.
3. Call `printLeaves` with the root's left and right children to print the
4. Call `printRightBoundary` with the right child of the root, excluding the
rightmost leaf.
Boundary Traversal Method
Algorithm:
4. printLeftBoundary(node):
Algorithm:
5. printRightBoundary(node):
Algorithm:
6. printLeaves(node):
Pseudocode:
procedure boundaryTraversal(root):
print root.val
printLeftBoundary(root.left)
printLeaves(root.left)
printLeaves(root.right)
printRightBoundary(root.right)
Boundary Traversal Method
Pseudocode:
procedure printLeftBoundary(node):
print node.val
printLeftBoundary(node.left)
if node.left is null:
printLeftBoundary(node.right)
Boundary Traversal Method
Pseudocode:
procedure printRightBoundary(node):
if node.right is null:
printRightBoundary(node.left)
printRightBoundary(node.right)
print node.val
Boundary Traversal Method
Pseudocode:
procedure printLeaves(node):
printLeaves(node.left)
if isLeaf(node):
print node.val
printLeaves(node.right)
function isLeaf(node):
return node is not null and node.left is null and node.right is null
Boundary Traversal Method
EXPLANATION
This Java code defines a binary tree node class and a program to perform
The boundaryTraversal method prints the root, left leaves, and right leaves,
The main function constructs an example tree and calls the boundary
traversal method.
Boundary Traversal Method
Time Complexity:
Space Complexity:
O(h) where h is the height of the tree (O(n) in the worst case,
Answer: Binary tree boundary traversal involves three main steps. First,
traverse and print the left boundary of the tree (excluding the leftmost
leaf). Second, print all leaf nodes from left to right. Lastly, traverse
and print the right boundary of the tree (excluding the rightmost leaf).
INTERVIEW QUESTION
Answer:
the number of nodes in the tree. This is because each node is visited
3. How would you modify the boundary traversal algorithm to include only
distinct values in the output?
Answer: To ensure only distinct values are printed, we can use a set or
and iteratively enqueue or push the nodes related to the left and right