Help the wicked witch!!
Given a binary tree, return the id of the branch containing a poisoned apple.
Each node has the following structure:
Node: {
id: int
poisoned: true/false
left: Node
right: Node
}
If no poisoned apple on the tree return -1.
const findPoisoned = (node) => {
// Help evil prevail !
return node == null || node == undefined ? -1
: node.poisoned ? node.id
: Math.max(findPoisoned(node.left),findPoisoned(node.right));
}
// TODO: Add your tests here
// Starting from Node 10.x, [Mocha](https://mochajs.org) is used instead of our custom test framework.
// [Codewars' assertion methods](https://github.com/Codewars/codewars.com/wiki/Codewars-JavaScript-Test-Framework)
// are still available for now.
//
// For new tests, using [Chai](https://chaijs.com/) is recommended.
// You can use it by requiring:
// const assert = require("chai").assert;
// If the failure output for deep equality is truncated, `chai.config.truncateThreshold` can be adjusted.
describe("Solution", function() {
it("should test for something", function() {
Test.assertEquals(findPoisoned({id:2, poisoned:true}), 2);
Test.assertEquals(findPoisoned({id:2, poisoned:false}), -1);
Test.assertEquals(findPoisoned({id:2, poisoned:false, left:{id:4 , poisoned:true} }), 4);
Test.assertEquals(findPoisoned({id:2, poisoned:false, right:{id:4 , poisoned:true} }), 4);
});
});