LeetCode – Power of Three (Java)

Given an integer, write a function to determine if it is a power of three. Java Solution 1 – Iteration public boolean isPowerOfThree(int n) { if(n==1) return true;   boolean result = false;   while(n>0){ int m = n%3; if(m==0){ n=n/3; if(n==1) return true; }else{ return false; } }   return result; }public boolean isPowerOfThree(int … Read more

LeetCode – Binary Search Tree Iterator (Java)

Problem Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree. … Read more