Boundary of Binary Tree

Anti-clockwise boundary: left → leaves → right (reversed)

Read Here
Step0/14
PhaseStart Boundary
Boundary[]
Back To Trees List

Python Code

7class Solution:
def boundaryOfBinaryTree(self, root):
9 if not root:
......
11 result = []
12 # Add root if not leaf
13 if not self.isLeaf(root):
......
16 self.addLeftBoundary(root.left, result)
17 # Collect all leaves (left-to-right)
18 self.addLeaves(root, result)
19 # Collect right boundary (bottom-up, no leaves)
20 self.addRightBoundary(root.right, result)
21 return result
22
......
27 while node:
28 if not self.isLeaf(node):
29 result.append(node.val)
......
35 if self.isLeaf(node):
36 result.append(node.val)
37 return
......
43 while node:
44 if not self.isLeaf(node):
45 stack.append(node.val)
......
47 # Add in reverse (bottom-up)
48 while stack:
49 result.append(stack.pop())
Current Line (8): Function Entry

Tree Structure

1234567891011
Operation:LEFT

Boundary Progress

Current Node

-

Phase

Start Boundary

Boundary Result

Boundary values appear here...
Step 1: LEFT

Call Stack

Stack is empty. Click Next to begin!

Step Explanation

Ready to Start

Click "Next Step" to begin boundary traversal in anti-clockwise order.

  • > Order: root -> left boundary -> leaves -> reversed right boundary.
  • > Watch phase and result update together.
Unvisited
Left
Current
Right
Done