二叉树的四种遍历(前中后序和层序)及其基础操作

二叉树

在这里插入图片描述

前,中,后序遍历

前序(先序):根 左 右
中序:左 根 右
后续:左 右 根

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+" ");
    }

层序遍历

思想:
在这里插入图片描述

public void levelOrder(Node root){
        Queue<Node> queue=new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            Node cur=queue.poll();
            System.out.print(cur.val+" ");
            if(cur.left!=null){
                queue.offer(cur.left);
            }
            if(cur.right!=null){
                queue.offer(cur.right);
            }
        }
    }

求节点个数

 public static int size(Node root){
        if(root==null){
            return 0;
        }
        //结点个数等于根节点+左子树的节点个数+右子树的节点个数
        return 1+size(root.left)+size(root.right);
    }

求叶子结点个数

//遍历树,将问题拆分成求root.left的叶子节点个数+root.right的叶子节点个数
    public static int leftSize(Node root){
        if(root==null){
            return 0;
        }
        if(root.left==null&&root.right==null){
            return 1;
        }
        return leftSize(root.left)+leftSize(root.right);
    }

求第K层的节点个数

  //如果K<1,只能是空树,直接返回0
    //如果k==1;说明只有一个根节点,返回1
    //第k层的节点个数=左子树的k-1层节点个数+右子树的(k-1)节点个数
    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);
    }

查找指定元素

 //如果找到了返回引用,如果不存在返回null
    Node find(Node root,int val){
        if(root==null){
            return null;
        }
        if(root.val==val){
            return root;
        }
        Node result=find(root.left,val);
        if(result!=null){
            return result;
        }
        return find(root.right,val);
    }

构建二叉树

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;
    }
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值