Ad
How Do I Remove A Specific Leaf Node With Index Value X And Display The Remaining Leaf Nodes?
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.
Ad
Answer
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);
}
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad