Created
March 29, 2019 20:40
-
-
Save codesnakers/3445d217b02eef8bc051fc3791ef88eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
Solution for: http://codesnakers.com/projects/2.png | |
http://codesnakers.com/projects/3.png | |
*/ | |
function solution(T) { | |
// write your code in JavaScript (Node.js 8.9.4) | |
var paths = []; | |
var keyArr = []; | |
var maxDistinct = 0; | |
var countDistinct = 0; | |
function getPaths(root, pathArr, pathCnt){ | |
if (null === root) | |
return; | |
pathArr[pathCnt] = root.x; | |
// If leaf then Print the Path | |
if (root.l == null && root.r == null){ | |
/*console.log("\n"); // to print paths in separate line*/ | |
paths.push([]); | |
for(var i=0; i<=pathCnt; i++){ | |
/*console.log(pathArr[i]);*/ | |
paths[paths.length-1].push(pathArr[i]); | |
} | |
}else{ | |
getPaths(root.l, pathArr, pathCnt + 1); | |
getPaths(root.r, pathArr, pathCnt + 1); | |
} | |
return pathArr; | |
} | |
getPaths(T, [], 0); | |
for(var i=0; i<paths.length; i++){ | |
keyArr[i] = []; | |
for(var j=0; j<paths[i].length;j++){ | |
keyArr[i][paths[i][j]] = true; | |
} | |
} | |
for(var i=0; i<keyArr.length; i++){ | |
countDistinct = 0; | |
for(var j=0; j<keyArr[i].length;j++){ | |
if(keyArr[i][j]){ | |
countDistinct++; | |
} | |
} | |
if(countDistinct>maxDistinct){ | |
maxDistinct = countDistinct; | |
} | |
} | |
/*console.log(keyArr, maxDistinct);*/ | |
return maxDistinct; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment