java-二叉树前序中序后序遍历的实现;求二叉树节点个数;叶子节点的个数;第k层节点的个数;二叉树查找指定元素

package TestTree;

/**
 * @program: 1012
 * @description
 * @author: YouName
 * @create: 2021-10-12 19:22
 **/
class Node {
    public char val;
    public Node left;
    public Node right;

    public Node(char val) {
        this.val = val;
    }
}
public class TestTree {
    public static Node build() {
        //手动把一颗树构造出来
        Node a = new Node('A');
        Node b = new Node('B');
        Node c = new Node('C');
        Node d = new Node('D');
        Node e = new Node('E');
        Node f = new Node('F');
        Node g = new Node('G');
        Node h = new Node('H');

        a.left = b;
        a.right = c;
        b.left = d;
        b.right = e;
        e.left = g;
        g.right = h;
        c.right = f;

        return a;
    }

    public static void preOrder(Node root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    public static void inOrder(Node root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    public static void postOrder(Node root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }

    //1.写一个方法,求二叉树中节点的个数
    //执行顺序和后序遍历的顺序一样
    public static int size(Node root) {
        if (root == null) {
            return 0;
        }
        return 1+size(root.left)+size(root.right);
    }

    //2.求二叉树叶子节点的个数
    //遍历树,拆分问题
    //root叶子节点的个数 = root.left的叶子节点个数 + root.right的叶子节点的个数
    public static int leafSize(Node root) {
        if (root == null) {
            return 0;
        }
        if (root.right == null && root.left == null) {
            //此时的root没有子树,root自己就是叶子节点
            return 1;
        }
        return leafSize(root.left) + leafSize(root.right);
    }

    //3.求二叉树第k层节点的个数
    //如果k < 1只能是空树,直接返回0;
    //如果k == 1,直接返回1;
    //k层节点的个数 = 左子树的 k-1 层节点的个数 + 右子树的 k-1 层节点的个数
    //针对A求k = 3节点个数 == 针对B求k = 2的节点个数 + 针对C求k = 2的节点个数
    public static int klevelSize(Node root,int k) {
        if (k < 1 || root == null) {
            return 0;
        }
        if (k ==1) {
            return 1;
        }
        return klevelSize(root.left,k-1) + klevelSize(root.right,k-1);
    }

    //4.在二叉树中查找指定元素--核心还是遍历
    //如果存在,返回该节点的引用,如果不存在,返回null
        Node find(Node root,char toFind) {
            if (root == null) {
                return null;
            }
            //此处的访问就是一个比较操作了
            if (root.val == toFind) {
                return root;
            }
            //分别递归地去查找左右子树
            Node result = find(root.left,toFind);
            if (result != null) {
                return result;
            }
            return find(root.right,toFind);
        }
    }



    public static void main(String[] args) {
        Node root = build();
        System.out.println("先序遍历");
        preOrder(root);
        System.out.println();
        System.out.println("中序遍历");
        inOrder(root);
        System.out.println();
        System.out.println("后序遍历");
        postOrder(root);
        System.out.println();
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值