Created
August 10, 2017 02:44
-
-
Save cixuuz/a2af4bce4cae3808019e4b823c492597 to your computer and use it in GitHub Desktop.
[222. Count Complete Tree Nodes] #lc0222
This file contains hidden or 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 Solution { | |
| public int countNodes(TreeNode root) { | |
| if (root == null) | |
| return 0; | |
| int lh = height(root.left); | |
| int rh = height(root.right); | |
| if (lh == rh) return (1 << lh) + countNodes(root.right); | |
| else return (1 << rh) + countNodes(root.left); | |
| } | |
| private int height(TreeNode node) { | |
| if (node == null) return 0; | |
| return 1 + height(node.left); | |
| } | |
| }222. Count Complete Tree Nodes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment