Skip to content

Instantly share code, notes, and snippets.

@sogwiz
Created July 10, 2019 18:58
Show Gist options
  • Save sogwiz/917f1db7fcebd79cc47a19f513f9bdcd to your computer and use it in GitHub Desktop.
Save sogwiz/917f1db7fcebd79cc47a19f513f9bdcd to your computer and use it in GitHub Desktop.
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