将整数数组转换为二叉树

时间:2014-11-12 16:26:29

标签: java algorithm binary-tree

我已经可以使用java中的以下算法将数组转换为二叉树:

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

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

public TreeNode arrayToTree(Integer[] input){
    TreeNode root = createTreeNode(input,1);
    return root;
}

private TreeNode createTreeNode(Integer[] input, int index){
    if(index<=input.length){
        Integer value = input[index-1];
        if(value!=null){
            TreeNode t = new TreeNode(value);
            t.left = createTreeNode(input, index*2);
            t.right = createTreeNode(input, index*2+1);
            return t;
        }
    }
    return null;
}

当输入为 {1,null,2,null,null,3} 时,我得到以下树:

1        
 \   
  2    
 /   
3

但我认为输入 {1,null,2,3} 足够清晰,可以定义如上所述的树。

避免在输入数组中定义多余的 nulls 有什么好主意吗?

3 个答案:

答案 0 :(得分:3)

这是一个Java怪兽,它可以调试并解决任务


import java.util.*;

public class TreeCreator {

    public static void main(String[] args) {
        Integer[] tree = new Integer[]{1, null, 2, 3};
        TreeCreator tr = new TreeCreator();
        TreeNode treeNode = tr.fromArray(tree);
        List<Integer> list = tr.postorderTraversal(treeNode);
        list.forEach(System.out::println); // postOrder is 3 2 1

    }

    public TreeNode fromArray(Integer[] tree) {
        if (tree.length == 0) return null;
        TreeNode root = new TreeNode(tree[0]);
        Queue<TreeNode> q = new LinkedList<>();
        q.add(root);
        for (int i = 1; i < tree.length; i++) {
            TreeNode node = q.peek();
            if (node.left == null) {
                node.left = new TreeNode(tree[i]);
                if (tree[i] != null) q.add(node.left);
            } else if (node.right == null) {
                node.right = new TreeNode(tree[i]);
                if (tree[i] != null) q.add(node.right);
                q.remove();
            }
        }
        return root;
    }

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

        TreeNode(Integer x) {
            val = x;
        }
    }

    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> l = new ArrayList<>();
        if (root == null) return l;
        funcPostOrder(root, l);
        return l;
    }

    private void funcPostOrder(TreeNode c, List<Integer> l) {
        if (c.left != null && c.left.val != null) {
            funcPostOrder(c.left, l);
        }
        if (c.right != null) {
            funcPostOrder(c.right, l);
        }
        l.add(c.val);
    }
}

更多有趣的例子是

Integer[] tree = new Integer[]{5,4,8,11,null,13,4,7,2,null,null,null,1};

答案 1 :(得分:1)

如果您预先阅读了树,您会发现1, -, 2, 3, -。只需使用相同的顺序构建树,而不是在index*2index*2+1上查找字符串,而是从左到右。 (如果您愿意,可以丢弃最终的空值。)

对于更“复杂”的例子:

       1
     /   \
   2       3
    \     / \
     4   5   6
          7   8

1, 2, -, 4, 3, 5, -, 7, 6, -, 8

答案 2 :(得分:0)

这应该可以解决问题。

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

TreeNode() {
}

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

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

static TreeNode arrayToTree(Integer array[]) {
    return arrayToTree(array, 0);
}

static TreeNode arrayToTree(Integer array[], int index) {
    if (index >= array.length)
        return null;
    if (array[index] == null)
        return null;
    return new TreeNode(array[index], arrayToTree(array, index * 2 + 1), arrayToTree(array, index * 2 + 2));
}