java实现一颗二叉树,完成层次遍历,两种中序遍历(递归,非递归)

```java
package com.easy.Tree;

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

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

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

    public TreeNode() {
    }

    public int getVal() {
        return val;
    }

    public void setVal(int val) {
        this.val = val;
    }

    public TreeNode getLeft() {
        return left;
    }

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

    public TreeNode getRight() {
        return right;
    }

    public void setRight(TreeNode right) {
        this.right = right;
    }
}
```

```java
package com.easy.Tree;

public class tree {
    TreeNode root=null;
}
```

```java
package com.easy.Tree;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class test {
    public static void main(String[] args) {
        tree tree = new tree();
        TreeNode treeNode = new TreeNode(10);
        tree.root = treeNode;
        TreeNode treeNode1 = new TreeNode(6);
        TreeNode treeNode2 = new TreeNode(7);
        TreeNode treeNode3 = new TreeNode(8);
        TreeNode treeNode4 = new TreeNode(9);
        tree.root.left = treeNode1;
        tree.root.right = treeNode2;
        treeNode1.left = treeNode3;
        treeNode2.right = treeNode4;
        inorderTraversal(tree.root);
        leve(tree.root);
        inorderTraversalStack(tree.root);


    }
    public static void leve(TreeNode root){
        if(root==null){
            System.out.println("无元素");
            return;
        }
        Queue<TreeNode> queue=new LinkedList<>();
        queue.add(root);
        int leve=1;
        while (!queue.isEmpty()){
            int len=queue.size();
            while (len>0){
                TreeNode temp=queue.poll();
                System.out.println("第"+leve+"层的元素为:"+temp.val);
                len--;
                if(temp.left!=null) queue.add(temp.left);
                if(temp.right!=null) queue.add(temp.right);
            }
            leve++;
        }
    }
    public static void inorderTraversal(TreeNode root){
        if(root==null){
            return;
        }
        inorderTraversal(root.left);
        System.out.println(root.val);
        inorderTraversal(root.right);
    }
    // 中序遍历顺序: 左-中-右 入栈顺序: 左-右
    // 处理中时弹出栈
    public static void inorderTraversalStack(TreeNode root){
        if(root==null){
            return;
        }
        Stack<TreeNode> s=new Stack<>();
        TreeNode cur=root;
        while (cur!=null||!s.empty()){
            if(cur!=null){
                s.push(cur);
                cur=cur.left;
            }else {
                cur=s.pop();
                System.out.println(cur.val);
                cur=cur.right;
            }
        }
    }
}
```

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值