Created
July 10, 2019 18:58
-
-
Save sogwiz/917f1db7fcebd79cc47a19f513f9bdcd 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
package com.learning.leet.tree; | |
/** | |
* Created by sargonbenjamin on 7/10/19. | |
* https://leetcode.com/problems/count-complete-tree-nodes/ | |
*/ | |
public class CountCompleteTreeNodez { | |
public int countNodes(TreeNode root) { | |
if(root==null)return 0; | |
int leftMost = nodeDepthLeft(root); | |
int rightMost = nodeDepthRight(root); | |
if(leftMost==rightMost)return (int)Math.pow(2,leftMost) -1; | |
return countNodes(root.left) + countNodes(root.right) + 1; | |
} | |
public int nodeDepthLeft(TreeNode root){ | |
if(root==null)return 0; | |
return 1 + nodeDepthLeft(root.left); | |
} | |
public int nodeDepthRight(TreeNode root){ | |
if(root==null)return 0; | |
return 1 + nodeDepthLeft(root.right); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment