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.
- Prepare
- Algorithms
- Implementation
- Utopian Tree
- Discussions
Utopian Tree
Utopian Tree
+ 0 comments RECURSION
int utopianTree(int n) { if(n==0) return 1; if(n%2==0) return 1 + utopianTree(n-1); return 2 * utopianTree(n-1); }
+ 0 comments c#
int height = 1; for (int i = 1; i <= n; i++) { if (i % 2 == 1) // nieparzyste { height *= 2; } else // parzyste { height += 1; } } return height;
+ 0 comments def utopianTree(n): h = 1 for i in range(n): if i % 2 == 0: h *= 2 else: h += 1 return h
+ 0 comments class Result {
/* * Complete the 'utopianTree' function below. * * The function is expected to return an INTEGER. * The function accepts INTEGER n as parameter. */ public static int utopianTree(int n) { // Write your code here ArrayList arr=new ArrayList<Integer>(); int count=0; for(int i=0;i<=n;i++) { if(i%2==0) { count+=1; arr.add(i, count); } else { count=count*2; arr.add(i, count); } } int wawa=(int)arr.get(n); return wawa; }
}
+ 0 comments Simple no brainer Python 3 solution:
def utopianTree(n): count = 0 for i in range(n+1): if i == 0: count += 1 elif i % 2 == 0: count += 1 else: count *= 2 return count
Load more conversations
Sort 1757 Discussions, By:
Please Login in order to post a comment