面试手撕代码:二叉树的递归遍历和非递归遍历(附详细例子)

public class BinaryTree {
    private TreeNode root;

    public BinaryTree() {
    }

    public BinaryTree(TreeNode root) {
        this.root = root;
    }

    public void setRoot(TreeNode root) {
        this.root = root;
    }

    public TreeNode getRoot() {
        return root;
    }

   static class TreeNode {
        private String data = null;
        private TreeNode left;
        private TreeNode right;

        public TreeNode(String data, TreeNode left, TreeNode right) {
            this.data = data;
            this.left = left;
            this.right = right;
        }

        public String getData() {
            return data;
        }

        public TreeNode getLeft() {
            return left;
        }

        public TreeNode getRight() {
            return right;
        }

        public void setData(String data) {
            this.data = data;
        }

        public void setLeft(TreeNode left) {
            this.left = left;
        }

        public void setRight(TreeNode right) {
            this.right = right;
        }
    }
    //先序遍历
    public void preOrder(TreeNode node){
        if(node!=null){
            System.out.println(node.getData()+" ");
            preOrder(node.getLeft());
            preOrder(node.getRight());
        }
    }
    //中序遍历
    public void inOrder(TreeNode node){
        if(node!=null){
            inOrder(node.getLeft());
            System.out.println(node.getData()+" ");
            inOrder(node.getRight());
        }
    }
    //后序遍历
    public void postOrder(TreeNode node){
        if(node!=null){
            postOrder(node.getLeft());
            postOrder(node.getRight());
            System.out.println(node.getData()+" ");
        }
    }
    public void preOrderNoRecursion(){
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        TreeNode current = null;
        while (!stack.isEmpty())
        {
            current = stack.pop();
            System.out.print(current.data+"  ");
            if (current.getRight() != null)
                stack.push(current.getRight());
            if (current.getLeft() != null)
                stack.push(current.getLeft());
        }
        System.out.println();

    }
    public void inOrderNoRecursion(){
        Stack<TreeNode> stack = new Stack<>();
        TreeNode current = root;
        while (current != null || !stack.isEmpty())
        {
            while(current != null)
            {
                stack.push(current);
                current = current.getLeft();
            }
            if (!stack.isEmpty())
            {
                current = stack.pop();
                System.out.print(current.data+"  ");
                current = current.getRight();
            }
        }

    }
    public void postorderNoRecursion()
    {
        TreeNode rNode = null;
        TreeNode current = root;
        Stack<TreeNode> stack = new Stack<>();
        while(current != null || !stack.isEmpty())
        {
            while(current != null)
            {
                stack.push(current);
                current = current.getLeft();
            }
            current = stack.pop();
            while (current != null && (current.getRight() == null ||current.getRight() == rNode))
            {
                System.out.print(current.data+"  ");
                rNode = current;
                if (stack.isEmpty())
                {
                    System.out.println();
                    return;
                }
                current = stack.pop();
            }
            stack.push(current);
            current = current.getRight();
        }
    }
    public static void main(String[] args){
        TreeNode l2 = new TreeNode("E", null, null);
        TreeNode r2 = new TreeNode("D", null, null);
        TreeNode l1 = new TreeNode("B", null, r2);
        TreeNode r1 = new TreeNode("C", l2, null);
        TreeNode root = new TreeNode("A", l1, r1);
        BinaryTree bt = new BinaryTree(root);
        System.out.print(" 递归 前序遍历------->");
        bt.preOrder(bt.getRoot());
        System.out.print(" 递归 中序遍历------->");
        bt.inOrder(bt.getRoot());
        System.out.print(" 递归 后序序遍历------->");
        bt.postOrder(bt.getRoot());
        System.out.println("------------------------------");
        System.out.print(" 非递归 前序遍历------->");
        bt.preOrderNoRecursion();
        System.out.print(" 非递归 中序序遍历------->");
        bt.inOrderNoRecursion();
        System.out.print(" 非递归 后序序遍历------->");
        bt.postorderNoRecursion();
    }
}

递归 前序遍历------->A
B
D
C
E
 递归 中序遍历------->B
D
A
E
C
 递归 后序序遍历------->D
B
E
C
A
------------------------------
 非递归 前序遍历------->A  B  D  C  E  
 非递归 中序序遍历------->B  D  A  E  C  

非递归 后序序遍历------->D  B  E  C  A  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值