创建如下的一棵树,然后进行遍历,并求树的节点数,叶子节点数,层数以及每层有多少个节点
class BTNode {
public char val;
public BTNode left;//左子树的引用
public BTNode right;//右子树的引用
public BTNode(char val) {
this.val = val;
}
}
public class BinaryTree {
public BTNode createTree() {
BTNode A = new BTNode('A');
BTNode B = new BTNode('B');
BTNode C = new BTNode('C');
BTNode D = new BTNode('D');
BTNode E = new BTNode('E');
BTNode F = new BTNode('F');
BTNode G = new BTNode('G');
BTNode H = new BTNode('H');
A.left = B;
A.right = C;
B.left = D;
B.right = E;
E.right = H;
C.left = F;
C.right = G;
return A;
}
// 前序遍历
void preOrderTraversal(BTNode root) {
if(root == null) return;
System.out.print(root.val);
preOrderTraversal(root.left);
preOrderTraversal(root.right);
}
// 中序遍历
void inOrderTraversal(BTNode root) {
if(root == null) return;
inOrderTraversal(root.left);
System.out.print(root.val);
inOrderTraversal(root.right);
}
// 后序遍历
void postOrderTraversal(BTNode root) {
if(root == null) return;
postOrderTraversal(root.left);
postOrderTraversal(root.right);
System.out.print(root.val);
}
// 遍历思路-求结点个数
static int size = 0;
void getSize1(BTNode root) {
if(root == null) return;
size++;
getSize1(root.right);
getSize1(root.left);
}
// 子问题思路-求结点个数
int getSize2(BTNode root){
if(root == null) return 0;
return getSize2(root.left) + getSize2(root.right) + 1;
}
// 遍历思路-求叶子结点个数
static int leafSize = 0;
void getLeafSize1(BTNode root){
if(root == null) return;
if(root.left == null && root.right == null) {
leafSize++;
}
getLeafSize1(root.left);
getLeafSize1(root.right);
}
// 子问题思路-求叶子结点个数
int getLeafSize2(BTNode root){
if(root == null) return 0;
if(root.left == null && root.right == null) {
return 1;
}
return getLeafSize2(root.left) + getLeafSize2(root.right);
}
// 子问题思路-求第 k 层结点个数
int getKLevelSize(BTNode root,int k) {
if(root == null) {
return 0;
}
if(k == 1) {
return 1;
}
return getKLevelSize(root.left,k-1)+
getKLevelSize(root.right,k-1);
}
// 获取二叉树的高度
public int maxDepth(TreeNode root) {
if(root==null) return 0;
int leftdepth = maxDepth(root.left);
int rightdepth = maxDepth(root.right);
return leftdepth>rightdepth? leftdepth +1 :rightdepth +1;
}
BTNode find(BTNode root, char val) {
if (root==null) return null;
if(root.val==val){
return root;
}
BTNode ret=find( root.left,val);
if(ret!=null){
return ret;
}
ret=find( root.right,val);
if(ret!=null){
return ret;
}
return null;
}
}