Skip to content
DeepForgeby svx
All problems
EasyData Structuresds-100

Tree Boundary Count

Problem

Count the nodes on the boundary of a binary tree.

tree is a level-order list with None marking missing nodes. The boundary consists of the root, the left boundary (excluding leaves), all leaves from left to right, and the right boundary (excluding leaves).

Return the number of boundary nodes.

Hint

Count the root, the non-leaf nodes on the left and right spines, and every leaf once.

Test cases

case 1
in: [[1,2,3,null,5,null,4]]
expected: 5
case 2
in: [[1,2,3,4,5,6,7]]
expected: 7
case 3
in: [[]]
expected: 0
case 4
in: [[1]]
expected: 1
case 5
in: [[1,2]]
expected: 2
Starter codePython
def count_boundary(tree):
    # Your code here
    pass

5 test cases · run your code against them in the editor