@oneexy
The reason of using mid = start+(endstart)/2 is:
because sometimes when you want to find mid of x and y such that x and y are very large numbers so it goes out of the range of int.
G
Gobu07
@Gobu07
2
Reputation
24
Posts
165
Profile views
0
Followers
0
Following
Posts made by Gobu07

RE: Super simple and clean Java, binary search.

RE: Concise Java solutions O(log(n)^2)
A very legible Solution for beginers
class Solution { public int countNodes(TreeNode root) { if(root == null) return 0; int left = getHeightLeft(root); int right = getHeightRight(root); //If left and right are equal it means that the tree is complete and hence go for 2^h 1. if(left == right) return ((2<<(left)) 1); //else recursively calculate the number of nodes in left and right and add 1 for root. else return countNodes(root.left)+ countNodes(root.right)+1; } public int getHeightLeft(TreeNode root){ int count=0; while(root.left!=null){ count++; root = root.left; } return count; } public int getHeightRight(TreeNode root){ int count=0; while(root.right!=null){ count++; root = root.right; } return count; } }

When U have a good legible Solution (JAVA)
class Solution { public int countNodes(TreeNode root) { if(root == null) return 0; int left = getHeightLeft(root); int right = getHeightRight(root); //If left and right are equal it means that the tree is complete and hence go for 2^h 1. if(left == right) return ((2<<(left)) 1); //else recursively calculate the number of nodes in left and right and add 1 for root. else return countNodes(root.left)+ countNodes(root.right)+1; } public int getHeightLeft(TreeNode root){ int count=0; while(root.left!=null){ count++; root = root.left; } return count; } public int getHeightRight(TreeNode root){ int count=0; while(root.right!=null){ count++; root = root.right; } return count; } }