http://algorithms.tutorialhorizon.com/find-the-size-of-a-binary-tree-without-recursion/
public static int getSize(Node root) {
if (root != null) {
int size=0; // size of tree
Queue<Node> q = new LinkedList<>();
// add root to the queue
q.add(root);
while (!q.isEmpty()) {
Node x = q.poll();
//increment the size
size++;
//add children to the queue
if(x.left!=null){
q.add(x.left);
}
if(x.right!=null){
q.add(x.right);
}
}
return size;
}
// if root is null, return 0
return 0;
}
http://algorithms.tutorialhorizon.com/find-the-size-of-the-binary-tree/
public int getSize(Node root){
if(root==null){
return 0;
}
return 1 + getSize(root.left) + getSize(root.right);
}
public static int getSize(Node root) {
if (root != null) {
int size=0; // size of tree
Queue<Node> q = new LinkedList<>();
// add root to the queue
q.add(root);
while (!q.isEmpty()) {
Node x = q.poll();
//increment the size
size++;
//add children to the queue
if(x.left!=null){
q.add(x.left);
}
if(x.right!=null){
q.add(x.right);
}
}
return size;
}
// if root is null, return 0
return 0;
}
http://algorithms.tutorialhorizon.com/find-the-size-of-the-binary-tree/
public int getSize(Node root){
if(root==null){
return 0;
}
return 1 + getSize(root.left) + getSize(root.right);
}