美文网首页
3. 二叉树

3. 二叉树

作者: codeMover | 来源:发表于2024-07-16 23:18 被阅读0次

    1.二叉树节点结构

    class Node<V>{
        V value;
        Node left;
        Node right;
    }
    

    用递归和非递归两种方式实现二叉树的先序、中序、后序遍历

    如果直观的打印一颗二叉树

    如何完成二叉树的宽度优先遍历(常见题目:求一颗二叉树的宽度)

    # 二叉树先序遍历
    public static void preOrderRecur(Node head){
        if(head == null){
            return;
        }
        System.out.print(head.value+" ");
        preOrderRecur(head.left);
        preOrderRecur(head.right);
    }
    
    # 二叉树中序遍历
    public static void inOrderRecur(Node head){
        if(head == null){
            return;
        }
        preOrderRecur(head.left);
        System.out.print(head.value+" ");
        preOrderRecur(head.right);
    }
    
    
    # 二叉树后序遍历
    public static void inOrderRecur(Node head){
        if(head == null){
            return;
        }
        preOrderRecur(head.left);
        preOrderRecur(head.right);
        System.out.print(head.value+" ");
    }
    

    相关文章

      网友评论

          本文标题:3. 二叉树

          本文链接:https://www.haomeiwen.com/subject/lsflhjtx.html