Java层次创建与遍历二叉树

数据结构:Java的层次创建与遍历,适合萌新~

禁止转载 侵权必究

不废话了直接上代码!

代码如下:

package tree;
/**
 * 第二题 层次创建和遍历
 */
import java.util.LinkedList;
import java.util.Queue;
import java.util.logging.Level;
 
class BinNode{
    Integer val;
    BinNode leftNode;//左孩子
    BinNode rightNode;//右孩子
    public BinNode(Integer val) {
        this.val = val;
    }
    public BinNode() {
    }
 
    public Integer getVal() {
        return val;
    }
    public void setVal(int val) {
        this.val = val;
    }
    public BinNode getLeftNode() {
        return leftNode;
    }
    public void setLeftNode(BinNode leftNode) {
        this.leftNode = leftNode;
    }
    public BinNode getRightNode() {
        return rightNode;
    }
    public void setRightNode(BinNode rightNode) {
        this.rightNode = rightNode;
    }
    
}
 
public class binTree {
    public static void main(String[] args) {
        Integer[] a = {3,9,20,null,null,15,7,null,null,null,null};
        int i=1;
        BinNode root = new BinNode(a[0]);  // 根节点
        BinNode current = null;
        Integer value = null;
        
        //层序创建二叉树
        LinkedList<BinNode> queue = new LinkedList<BinNode>(); 
        queue.offer(root);
        while(i<a.length) {
            current = queue.poll();//从链表中移除并获取第一个节点
            
            value = a[i++];
            if(value!=null) {
                BinNode left =new BinNode(value);
                current.setLeftNode(left);//创建当前节点的左孩子
                queue.offer(left); // 在链表尾部 左孩子入队
            }
            
            value=a[i++];
            if(value!=null) {
                BinNode right =new BinNode(value);
                current.setRightNode(right);//创建当前节点的右孩子
                queue.offer(right);// 在链表尾部 右孩子入队
            }
            
        }
        levelIetrator(root);
        
    }
    
    public static int levelIetrator(BinNode  root) {
        if(root==null) {
            return -1;
        }
        Queue<BinNode> queue = new LinkedList<BinNode>();
        BinNode current = null;
        queue.offer(root);
        while(!queue.isEmpty()) {
            current = queue.poll();
            if(current.getLeftNode()!=null) {
                queue.offer(current.getLeftNode());
                System.out.println("节点"+current.val+"的左孩子是"+current.getLeftNode().val);
            }else {
                System.out.println("节点"+current.val+"没有左孩子");
            }
            if(current.getRightNode()!=null) {
                queue.offer(current.getRightNode());
                System.out.println("节点"+current.val+"的右孩子是"+current.getRightNode().val);
            }else {
                System.out.println("节点"+current.val+"没有右孩子");
            }
        }
        return 1;
    }
}
 
 

运行结果:

节点3的左孩子是9
节点3的右孩子是20
节点9没有左孩子
节点9没有右孩子
节点20的左孩子是15
节点20的右孩子是7
节点15没有左孩子
节点15没有右孩子
节点7没有左孩子
节点7没有右孩子

宝贝们点个关注可以吗~~~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值