目录
定义一个二叉树
public class MyBinTree {
private static class TreeNode{
char val;
// 左子树根节点
TreeNode left;
// 右子树的根节点
TreeNode right;
public TreeNode(char val) {
this.val = val;
}
}
/**
* 创建一个二叉树
* @return
*/
public static TreeNode build(){
TreeNode nodeA = new TreeNode('A');
TreeNode nodeB = new TreeNode('B');
TreeNode nodeC = new TreeNode('C');
TreeNode nodeD = new TreeNode('D');
TreeNode nodeE = new TreeNode('E');
TreeNode nodeF = new TreeNode('F');
TreeNode nodeG = new TreeNode('G');
TreeNode nodeH = new TreeNode('H');
nodeA.left = nodeB;
nodeA.right = nodeC;
nodeB.left = nodeD;
nodeB.right = nodeE;
nodeE.right = nodeH;
nodeC.left = nodeF;
nodeC.right = nodeG;
return nodeA;
}
}
前序遍历
/**
* 前序遍历,传入一个二叉树的根节点,可以按照先序遍历的方式来输出节点值
* @param root
*/
public static void preOrder(TreeNode root){
if (root == null){
return;
}
System.out.print(root.val+"");
preOrder(root.left);
preOrder(root.right);
}
中序遍历
/**
* 中序遍历,传入一个二叉树的根节点,可以按照中序遍历的方式来输出节点值
* @param root
*/
public static void inOrder(TreeNode root){
if (root == null){
return;
}
inOrder(root.left);
System.out.print(root.val+"");
inOrder(root.right);
}
后序遍历
/**
* 后序遍历,传入一个二叉树的根节点,可以按照先序遍历的方式来输出节点值
* @param root
*/
public static void postOreder(TreeNode root){
if (root == null){
return;
}
preOrder(root.left);
preOrder(root.right);
System.out.print(root.val+"");
}
求二叉树的节点数
/**
* 传入一个二叉树的根节点,统计二叉树有多少个节点,返回节点数
* @param root
* @return
*/
public static int getNodes(TreeNode root){
if (root == null){
return 0;
}
return 1+getNodes(root.left)+getNodes(root.right);
}
求二叉树的叶子节点数
/**
* 传入一个二叉树的根节点,统计二叉树有多少个叶子节点
* @param root
* @return
*/
public static int getLeafNodes(TreeNode root){
if (root == null){
return 0;
}
if (root.left == null || root.right == null){
return 1;
}
return 1+getLeafNodes(root.left)+getLeafNodes(root.right);
}
查找二叉树中是否有值为val的节点
/**
* 查找二叉树中有没有值是val的节点
* @param root
* @param val
* @return
*/
public static boolean contains(TreeNode root,char val){
if (root == null){
return false;
}
if (root.val == val){
return true;
}
return contains(root.left,val)||contains(root.right,val);
}
求二叉树的高度
/**
* 传入一个二叉树的根节点,返回这个二叉树的高度
* @param root
* @return
*/
public static int height(TreeNode root){
if (root == null){
return 0;
}
return 1+Math.max(height(root.left),height(root.right));
}
求二叉树第k层的节点
/**
* 求以为root节点的第k层的节点个数
* @param root
* @param k
* @return
*/
public static int getKLevelNodes(TreeNode root,int k){
if (root == null || k <= 0){
return 0;
}
if (k == 1){
return 1;
}
return getKLevelNodes(root.left,k-1)+getKLevelNodes(root.right,k-1);
}
全部代码
/**
* @author happy
* 二叉树
*/
public class MyBinTree {
public static void main(String[] args) {
TreeNode root = build();
// 前序遍历:ABDEHCFG
System.out.print("前序遍历:");
preOrder(root);
System.out.println();
// 中序遍历:DBEHAFCG
System.out.print("中序遍历:");
inOrder(root);
System.out.println();
// 后序遍历:BDEHCFGA
System.out.print("后序遍历:");
postOreder(root);
System.out.println();
// 这个二叉树共有8个节点
System.out.println("这个二叉树共有"+getNodes(root)+"个节点");
// 这个二叉树共有7个叶子节点
System.out.println("这个二叉树共有"+getLeafNodes(root)+"个叶子节点");
// true
System.out.println(contains(root,'A'));
// false
System.out.println(contains(root,'I'));
// 这个二叉树的高度是:4
System.out.println("这个二叉树的高度是:"+height(root));
// 这个二叉树第3层的节点个数是:4
System.out.println("这个二叉树第3层的节点个数是:"+getKLevelNodes(root,3));
}
private static class TreeNode{
char val;
// 左子树根节点
TreeNode left;
// 右子树的根节点
TreeNode right;
public TreeNode(char val) {
this.val = val;
}
}
/**
* 创建一个二叉树
* @return
*/
public static TreeNode build(){
TreeNode nodeA = new TreeNode('A');
TreeNode nodeB = new TreeNode('B');
TreeNode nodeC = new TreeNode('C');
TreeNode nodeD = new TreeNode('D');
TreeNode nodeE = new TreeNode('E');
TreeNode nodeF = new TreeNode('F');
TreeNode nodeG = new TreeNode('G');
TreeNode nodeH = new TreeNode('H');
nodeA.left = nodeB;
nodeA.right = nodeC;
nodeB.left = nodeD;
nodeB.right = nodeE;
nodeE.right = nodeH;
nodeC.left = nodeF;
nodeC.right = nodeG;
return nodeA;
}
/**
* 前序遍历,传入一个二叉树的根节点,可以按照先序遍历的方式来输出节点值
* @param root
*/
public static void preOrder(TreeNode root){
if (root == null){
return;
}
System.out.print(root.val+"");
preOrder(root.left);
preOrder(root.right);
}
/**
* 中序遍历,传入一个二叉树的根节点,可以按照中序遍历的方式来输出节点值
* @param root
*/
public static void inOrder(TreeNode root){
if (root == null){
return;
}
inOrder(root.left);
System.out.print(root.val+"");
inOrder(root.right);
}
/**
* 后序遍历,传入一个二叉树的根节点,可以按照先序遍历的方式来输出节点值
* @param root
*/
public static void postOreder(TreeNode root){
if (root == null){
return;
}
preOrder(root.left);
preOrder(root.right);
System.out.print(root.val+"");
}
/**
* 传入一个二叉树的根节点,统计二叉树有多少个节点,返回节点数
* @param root
* @return
*/
public static int getNodes(TreeNode root){
if (root == null){
return 0;
}
return 1+getNodes(root.left)+getNodes(root.right);
}
/**
* 传入一个二叉树的根节点,统计二叉树有多少个叶子节点
* @param root
* @return
*/
public static int getLeafNodes(TreeNode root){
if (root == null){
return 0;
}
if (root.left == null || root.right == null){
return 1;
}
return 1+getLeafNodes(root.left)+getLeafNodes(root.right);
}
/**
* 查找二叉树中有没有值是val的节点
* @param root
* @param val
* @return
*/
public static boolean contains(TreeNode root,char val){
if (root == null){
return false;
}
if (root.val == val){
return true;
}
return contains(root.left,val)||contains(root.right,val);
}
/**
* 传入一个二叉树的根节点,返回这个二叉树的高度
* @param root
* @return
*/
public static int height(TreeNode root){
if (root == null){
return 0;
}
return 1+Math.max(height(root.left),height(root.right));
}
/**
* 求以为root节点的第k层的节点个数
* @param root
* @param k
* @return
*/
public static int getKLevelNodes(TreeNode root,int k){
if (root == null || k <= 0){
return 0;
}
if (k == 1){
return 1;
}
return getKLevelNodes(root.left,k-1)+getKLevelNodes(root.right,k-1);
}
}