博主主页: 码农派大星.
关注博主带你了解更多数据结构知识
1我们先来模拟创建一个二叉树
public class TestBinaryTreee {
static class TreeNode{
public char val;
public TreeNode left;
public TreeNode right;
public TreeNode(char val) {
this.val = val;
}
}
public TreeNode creatTree(){
TreeNode A = new TreeNode('A');
TreeNode B = new TreeNode('B');
TreeNode C = new TreeNode('C');
TreeNode D = new TreeNode('D');
TreeNode E = new TreeNode('E');
TreeNode F = new TreeNode('F');
TreeNode G = new TreeNode('G');
TreeNode H = new TreeNode('H');
A.left = B;
A.right = C;
B.left = D;
B.right = E;
C.left = F;
C.right = G;
E.right = H;
return A;
//就是根节点
}
}
2分别实现它的前中后序遍历
1前序遍历
// 前序遍历
void preOrder(TreeNode root){
if (root == null){
return;
}
System.out.print(root.val+" ");
//递归遍历左子树
preOrder(root.left);
//第归遍历右子树
preOrder(root.right );
}
TestBinaryTreee testBinaryTreee = new TestBinaryTreee();
testBinaryTreee.creatTree();
TestBinaryTreee.TreeNode root = testBinaryTreee.creatTree();
testBinaryTreee.preOrder(root);
System.out.println();
2中序遍历
// 中序遍历
void inOrder(TreeNode root){
if (root == null){
return;
}
inOrder(root.left);
System.out.print(root.val+" ");
inOrder(root.right);
}
testBinaryTreee.inOrder(root);
System.out.println();
3后序遍历
// 后序遍历
void postOrder(TreeNode root){
if (root == null){
return;
}
inOrder(root.left);
inOrder(root.right);
System.out.print(root.val+" ");
}
testBinaryTreee.postOrder(root);
System.out.println();
3 获取树中节点的个数
//求有多少个节点
1:
public int size(TreeNode root){
if (root == null){
return 0;
}
int ret = size(root.left)+
size(root.right)+1;
return ret;
}
2:
public static int nodeSize;
public void size2(TreeNode root){
if (root == null){
return ;
}
nodeSize++;
size2(root.left);
size2(root.right);
}
System.out.println(testBinaryTreee.size(root));
testBinaryTreee.size2(root);
System.out.println(TestBinaryTreee.nodeSize);
4获取叶子节点的个数
//求叶子结点个数
1:
public int getLeafNodeCount(TreeNode root){
if (root == null){
return 0;
}
if(root.left == null && root.right ==null){
return 1;
}
return getLeafNodeCount(root.left)+
getLeafNodeCount(root.right);
}
2:
public int leafSize;
public void getLeafNodeCount2(TreeNode root){
if (root == null){
return ;
}
if(root.left == null && root.right ==null){
leafSize++;
}
getLeafNodeCount2(root.left);
getLeafNodeCount2(root.right);
}
System.out.println(testBinaryTreee.getLeafNodeCount(root));
testBinaryTreee.getLeafNodeCount2(root);
System.out.println(testBinaryTreee.leafSize);
5 获取第K层节点的个数
//获取第k层节点个数
public int getKLevelNodeCount(TreeNode root,int k){
if(root == null){
return 0;
}
if (k==1){
return 1;
}
return getKLevelNodeCount(root.left,k-1)+
getKLevelNodeCount(root.right,k-1);
}
System.out.println(testBinaryTreee.getKLevelNodeCount(root, 3));
6获取二叉树的高度
//获取二叉树高度
public int getHeight(TreeNode root){
if(root == null){
return 0;
}
int leftHeight = getHeight(root.left);
int rightHeight = getHeight(root.right);
return leftHeight > rightHeight ?
leftHeight+1 : rightHeight+1;
}
int height = testBinaryTreee.getHeight(root);
System.out.println(height);
7检测值为value的元素是否存在
// 检测值为val的元素是否存在
public TreeNode find(TreeNode root,char val){
if(root == null){
return null;
}
if (root.val == val ){
return root;
}
TreeNode ret = find(root.left,val);
if (ret != null){
return ret;
}
ret = find(root.right,val);
if (ret != null){
return ret;
}
return null;
}
TestBinaryTreee.TreeNode retN = testBinaryTreee.find(root, 'E');
System.out.println(retN.val);