← Back to challenges

Binary Tree Nodes

PythonHardalgorithmsarraysconditionsinterview

Instructions

We have two lists _N and _P, where _N represents the value of a node in Binary Tree, and _P is the parent of _N.

NP
12
32
68
98
25
85
5-1

Write a function to find the node type of the node within this Binary Tree, ordered by the value of the node. Output one of the following:

  • Root: If node is root node.
  • Leaf: If node is leaf node.
  • Inner: If node is neither root nor leaf node.
  • Not exist: If node not exist.
node_type([1, 3, 6, 9, 2, 8, 5], [2, 2, 8, 8, 5, 5, -1], 5) ➞ "Root"

node_type([1, 3, 6, 9, 2, 8, 5], [2, 2, 8, 8, 5, 5, -1], 6) ➞ "Leaf"

node_type([1, 3, 6, 9, 2, 8, 5], [2, 2, 8, 8, 5, 5, -1], 2) ➞ "Inner"

node_type([1, 3, 6, 9, 2, 8, 5], [2, 2, 8, 8, 5, 5, -1], 10) ➞ "Not exist"

Binary Tree Example

Notes

All values of _N list are unique.

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.