数组转二叉树

数组转二叉树

思路:

  • 准备数组
  • 构建树节点集合
  • 循环树节点集合构建二叉树
public class BinaryTreeDemo {

	public static void main(String[] args) {
		//原始数组中可以输入null,转为的二叉树缺少左节点
		//Integer[] arr = {1,2,3,4,5,null,7};
		Integer[] arr = {1,2,3,4,5,6,7};
		// 数组转为二叉树
        TreeNode node = arrayToTree(arr);
        // 二叉树前序遍历一下
        node.preNode();
	}
	
	/**
     * 数组转为二叉树
     * @param arr   输入原始数组
     * @return  返回二叉树的跟节点
     */
	public static TreeNode arrayToTree(Integer[] arr){
        int len = arr.length;
        List<TreeNode> nodes = new ArrayList<>();
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == null) {
                nodes.add(null);
            } else {
                nodes.add(new TreeNode(arr[i]));
            }
        }

        for (int i = 0; i < len/2; i++) {
            TreeNode node = nodes.get(i);

            if (2*i+1 < len){
                TreeNode left = nodes.get(2*i+1);
                node.setLeft(left);
            }
            if (2*i+2 < len) {
                TreeNode right = nodes.get(2*i+2);
                node.setRight(right);
            }
        }
        return nodes.get(0);
    }
    
}

class TreeNode{
    private Integer val;
    private TreeNode left;
    private TreeNode right;

    public Integer getVal() {
        return val;
    }

    public void setVal(Integer 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;
    }

    public TreeNode () {}
    public TreeNode (Integer val) {
        this.val = val;
    }
    public TreeNode (Integer val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
        @Override
    public String toString() {
        return "TreeNode{" +
                "val=" + val +
                '}';
    }

//    前序遍历
    public void preNode(){
        System.out.println(this.val);
        if (this.left != null){
            this.left.preNode();
        }
        if (this.right != null){
            this.right.preNode();
        }
    }

//    中序遍历
    public void midNode(){
        if (this.left != null) {
            this.left.midNode();
        }
        System.out.println(this.val);
        if (this.right != null) {
            this.right.midNode();
        }
    }

//    后序遍历
    public void postNode(){
        if (this.left != null) {
            this.left.postNode();
        }
        if (this.right != null) {
            this.right.postNode();
        }
        System.out.println(this.val);
    }
 }   
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

难过的风景

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值