Last active
September 10, 2019 19:05
-
-
Save yokolet/64781fb9082954396c34272dd94ae705 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
public class TreeNode { | |
int val; | |
TreeNode left; | |
TreeNode right; | |
TreeNode(int x) { val = x; } | |
} | |
public class CBTNodeCount { | |
private int getHeight(TreeNode root) { | |
if (root == null) { return 0; } | |
return 1 + getHeight(root.left); | |
} | |
public int countNodes(TreeNode root) { | |
if (root == null) { return 0; } | |
int left = getHeight(root.left); | |
int right = getHeight(root.right); | |
if (left == right) { | |
return (int)Math.pow(2, left) + countNodes(root.right); | |
} else { | |
return (int)Math.pow(2, right) + countNodes(root.left); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment