//先利用 前序遍历 求 (前序,中序,后序,层序遍历都可以求)
//1. 利用遍历的思想解决
//2.利用汇总的思想解决
//先把树看成三部分组成: 根 + 左子树 + 右子树
import java.util.*;
public class Trees {
public static int count = 0;
public static void calcCount(Node root){ // 计算二叉树中结点的个数,利用遍历的思想求
if(root == null){
return;
}
count++;
calcCount(root.left);
calcCount(root.right);
}
public static void calcCount2(Node root) { // 计算二叉树中结点的个数,利用汇总的思想求
if (root == null) {
return;
}
int left = calcCount2(root.left);
int right = calcCount2(root.right);
int count = left + right + 1;
return count;
}
}