We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
If you pass test case 0 but fail other test cases, you might be encountering the overflow issue when multiplying two integers; the correct way is store result in a long variable
classProductOfRedNodesVisitorextendsTreeVis{privatelongproduct=1L;privatestaticfinalintMOD=1000000007;publicintgetResult(){return(int)product;}publicvoidvisitNode(TreeNodenode){if(node.getColor()==Color.RED){product=(product*node.getValue())%MOD;//overflow when int * int}}publicvoidvisitLeaf(TreeLeafleaf){if(leaf.getColor()==Color.RED){product=(product*leaf.getValue())%MOD;}}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Visitor Pattern
You are viewing a single comment's thread. Return to all comments →
If you pass test case 0 but fail other test cases, you might be encountering the overflow issue when multiplying two integers; the correct way is store result in a long variable