美文网首页
二叉树的先序,中序,后序遍历?

二叉树的先序,中序,后序遍历?

作者: 霍运浩 | 来源:发表于2019-04-21 18:49 被阅读0次

    题目描述

    二叉树的先序,中序,后序遍历

    代码实现

    public class Code_01_PreInPosTraversal {
    
        public static class Node {
            public int value;
            public Node left;
            public Node right;
    
            public Node(int data) {
                this.value = data;
            }
        }
          //先序遍历
        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;
            }
            inOrderRecur(head.left);
            System.out.print(head.value + " ");
            inOrderRecur(head.right);
        }
            //后序遍历 递归实现
        public static void postOrderRecur(Node head) {
            if (head == null) {
                return;
            }
            postOrderRecur(head.left);
            postOrderRecur(head.right);
            System.out.print(head.value + " ");
        }
    }
    

    相关文章

      网友评论

          本文标题:二叉树的先序,中序,后序遍历?

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