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 One of the possible way to do it Golang
func utopianTree(n int32) int32 { if n == 0 { return 1 } if n == 1 { return 2 } var h, i int32 = 1, 1 for i <= n { h = h * 2 i++ if i <= n { h = h + 1 i++ } } return h }
+ 0 comments Python3
def utopianTree(n): # Write your code here height=1 for i in range(0, n+1): if i==0: pass elif i % 2==0: height+=1 else: height*=2 return height
+ 0 comments def utopianTree(n): height = 1 if n > 0: for period in range(1, n+1): if period % 2 == 1: height *= 2 else: height += 1 return height
+ 0 comments Go
func utopianTree(n int32) int32 { var grow int32=1 for i:=int32(1);i<=n;i++{ if(i%2==0){ grow++ } else { grow=grow*2 } } return grow }
+ 0 comments using JS:
let currHeight = 1; for(let i = 1; i <= n; i++) { currHeight += (i%2 == 0)? 1:currHeight; } ** return currHeight; **
Load more conversations
Sort 1878 Discussions, By:
Please Login in order to post a comment