二叉树的前、中、后、层序遍历(递归和非递归)

package com.jp.ByteDance.mianshi0527;
import java.util.ArrayDeque;
import java.util.Deque;

import java.util.Stack;

/**
 * @program: mianjing
 * @description: 在二叉树中查找是否存在路径和为S的路径,且必须包含叶子节点
 * @author: CoderPengJiang
 * @create: 2020-05-27 22:05
 **/
public class FindSumOfValue {
    public static void main(String[] args) {
      TreeNode root1= new TreeNode(1);
        TreeNode root2= new TreeNode(2);
        TreeNode root3= new TreeNode(3);
        TreeNode root4= new TreeNode(4);
        TreeNode root5= new TreeNode(5);
        TreeNode root6= new TreeNode(6);
        TreeNode root7= new TreeNode(7);
        root1.left=root2;
        root1.right=root3;
        root2.left=root4;
        root2.right=root5;
        root3.left=root6;
        root3.right=root7;

        //采用递归进行先序遍历
        System.out.print("采用递归先序遍历:  ");
        preorderTraversalByRecursive(root1);

        System.out.println();
        //采用非递归进行先序遍历
        System.out.print("采用非递归先序遍历:  ");
        preorderTraversalByNoRecursive(root1);

        System.out.println();
        //采用递归进行中序遍历
        System.out.print("采用递归中序遍历:  ");
        midorderTraversalByRecursive(root1);

        System.out.println();
        //采用非递归进行中序遍历
        System.out.print("采用非递归中序遍历:  ");
        midorderTraversalByNoRecursive(root1);

        System.out.println();
        //采用递归进行后序遍历
        System.out.print("采用递归后序遍历:  ");
        postorderTraversalByRecursive(root1);

        System.out.println();
        //采用非递归进行后序遍历
        System.out.print("采用非递归后序遍历:  ");
        postorderTraversalByNoRecursive(root1);

        System.out.println();
        //采用非递归进行层序遍历
        System.out.print("采用非递归层序遍历:  ");
        sequenceTraversalByNoRecursive(root1);
    }
    public static  void preorderTraversalByRecursive(TreeNode root){
        if (root == null){
            return;
        }
        System.out.print(root.val+",");
        preorderTraversalByRecursive(root.left);
        preorderTraversalByRecursive(root.right);
    }

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

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

    }

    public static void midorderTraversalByNoRecursive(TreeNode root){
        if (root == null){
            return;
        }

        Deque<TreeNode> deque=new ArrayDeque<TreeNode> ();
        while (!deque.isEmpty()||root!=null) {
            //如果左子树不为空就先全部压进去
            if (root!=null){
                deque.push(root);
                root=root.left;
            }else {
                root=deque.pop();
                System.out.print(root.val+", ");
                root=root.right;
            }
        }

    }

    public static void preorderTraversalByNoRecursive(TreeNode root){
        if (root == null){
            return;
        }
        Deque<TreeNode> stack = new ArrayDeque<>();
        stack.push(root);
        while(!stack.isEmpty()){
            TreeNode cur= stack.pop();
            System.out.print(cur.val+",");
            if (cur.right!=null){
                stack.push(cur.right);
            }
            if (cur.left!=null){
                stack.push(cur.left);
            }
        }
    }

    public static void postorderTraversalByNoRecursive(TreeNode root){
        if (root == null){
            return;
        }
        //保存遍历时的节点信息,
        Deque<TreeNode> stack1 = new ArrayDeque<>();
        //排列后根的顺序
        Deque<TreeNode> stack2 = new ArrayDeque<>();
        stack1.push(root);
        while (!stack1.isEmpty()){
            root=stack1.pop();
            if (root.left!=null){
                stack1.push(root.left);
            }
            if (root.right!=null){
                stack1.push(root.right);
            }
            stack2.push(root);
        }
        while (!stack2.isEmpty()) {
            System.out.print(stack2.pop().val+", ");
        }
    }

    public static void sequenceTraversalByNoRecursive(TreeNode root){
        if (root==null){
            return;
        }
//        插入、删除、获取操作支持两种形式:快速失败和返回null或true/false
//        既具有FIFO特点又具有LIFO特点,即是队列又是栈
//        不推荐插入null元素,null作为特定返回值表示队列为空
//                未定义基于元素相等的equals和hashCode
        Deque<TreeNode> stack=new ArrayDeque<TreeNode> ();
        stack.add(root);
        while (!stack.isEmpty()) {
            TreeNode cur = stack.poll();
            System.out.print(cur.val+", ");
            if (cur.left!=null){
                stack.add(cur.left);
            }
            if (cur.right!=null){
                stack.add(cur.right);
            }
        }
    }

}


class TreeNode{
    int val;
    TreeNode left;
    TreeNode right;

    public TreeNode(int val) {
        this.val = val;
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值