# How Do I Remove A Specific Leaf Node With Index Value X And Display The Remaining Leaf Nodes?

## 27 April 2018 - 1 answer

Test case:

Input:

``````5
-1 0 0 1 1
2
``````

Output:

``````2
``````

Explanation:

The tree corresponding to the numeric input is shown above. Initially, there are 3 leaves, 2, 3 and 4 (marked green). If we delete node 2, then two leaves are left, i.e., 3 and 4. Hence, the answer is 2.

Another test case:

Input:

``````5
-1 0 0 1 1
1
``````

Output:

``````1
``````

Explanation:

After deleting node 1, the only remaining leaf is 2.

You create tree-structure from your input. Each node has this values: `node = { value, leftChild, rightChild }`

``````function trace(node, deletedValue) {
if (!node){
return; // nothing to see there
}

if (node.value === deletedValue) {
return; // this one is deleted, do not continue
}

if (!node.leftChild && !node.rightChild){
console.log(node.value); //found not deleted leaf
return;
}

trace(node.leftChild);
trace(node.rightChild);
}
``````