Learn
Practice
Interview
Resources
Newsletter
F
Toggle theme
0
F
0
Toggle menu
Animations
← Back to All Animations
Boundary of Binary Tree
1
boundaryOfBinaryTree(Node(1))
Simple Right-skewed
Standard Tree
Deep Tree
Small BST
Custom
tree
=
[1, null, 2, null, null, 3, 4]
1
2
root
left
leaves
right
boundary
boundary = root + left edge + leaves + right edge
1
2
root
left
leaves
right
boundary
boundary = root + left edge + leaves + right edge
1
2
root
left
leaves
right
boundary
1
root always starts the boundary
1
2
root
left
leaves
right
boundary
1
left edge: top-down, skip leaves
1
2
root
left
leaves
right
boundary
1
leaves: DFS left to right
1
2
root
left
leaves
right
boundary
1
is this node a leaf?
1
2
root
left
leaves
right
boundary
1
not a leaf: recurse
1
2
root
left
leaves
right
boundary
1
is this node a leaf?
1
2
root
left
leaves
right
boundary
1
2
leaf: boundary +=
2
1
2
root
left
leaves
right
boundary
1
2
right edge: collect top-down, add bottom-up
1
2
root
left
leaves
right
boundary
1
2
boundary = [1, 2]
Step:
Start: walk the tree's outline anticlockwise from the root
0 / 9
Simple Right-skewed
Standard Tree
Deep Tree
Small BST
Custom
tree
=
[1, null, 2, null, null, 3, 4]
0 / 9
Step:
Start: walk the tree's outline anticlockwise from the root
1
boundaryOfBinaryTree(Node(1))