0

How to construct a binary tree from a sequence of values.

Input: [4,9,0,5,1]
    4
   / \
  9   0
 / \
5   1

Input with null: [1,2,3,null,5,6,7]

       1
     /   \
    2     3
  /  \   /  \
null  5  6   7


Note: the tree is not a binary search tree. The nodes are inserted in pre order ( root, left, right ). Start by filling the child nodes from left to right. Once the level is filled, go to the next level. My intuition is to keep reference of the parent nodes.

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

    public TreeNode(int x) {
        value = x;
    }
}

public static TreeNode createTree(List<Integer> values) {
   // ???
   // return the root node
}

I feel a stupid asking this.

PS: I wondered how the tree was built from the inputs https://leetcode.com/problems/sum-root-to-leaf-numbers/

PS2 : Berto99 gave a draft of recursive way. Wondering about the iterative way (which will need to keep references of parent nodes)

1
  • How do you store null value to int value? Commented Jul 2, 2020 at 23:15

2 Answers 2

1

Taking an inspiration from the heaps:

public static TreeNode createTree(List<Integer> values, int index) {
   TreeNode tree = new TreeNode(values[index]);
   if(index * 2 < list.size())
       tree.left = createTree(values, index * 2);
   if(index * 2 + 1 < list.size())
       tree.right = createTree(values, index * 2 + 1);
   return tree;
}

Keep in mind that this works with the index that starts from 1, if you want the version that start from 1, you should use

public static TreeNode createTree(List<Integer> values, int index) {
   TreeNode tree = new TreeNode(values[index-1]); //   <<--- changed here
   if(index * 2 < list.size())
       tree.left = createTree(values, index * 2);
   if(index * 2 + 1 < list.size())
       tree.right = createTree(values, index * 2 + 1);
   return tree;
}

the logic is pretty simple, given an array, the root is the first element, the left child the pos*2 and the right the left child the pos*2 + 1 (again, first = 1, not 0)

Sign up to request clarification or add additional context in comments.

3 Comments

hoping that the code works, i've not wrote Java code in a while ahah
I get the idea, the indexes are iterated by recursion ( not in a loop). Then, to initialise TreeNode root = createTree(values,1);
@RaymondChenon exactly
0

@Berto99 gave the main idea

Visualize the tree with inputs [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]

             0
        /          \
      1             2
    /   \         /    \
  3       4      5       6 
 / \    /  \    /  \    /  \
7   8  9   10  11  12  13  14

Substitute the value 3 by index then you can infer the recurrence for left and right node.
node(3).left = 7 = 2 * index + 1
node(3).right = 8 = 2 * index + 2

The complete fine-tuned code is

    public static TreeNode createTree(List<Integer> values) {
        if (values == null || values.size() == 0) return null;
        TreeNode root = createTree(values, 0);
        return root;
    }

    private static TreeNode createTree(List<Integer> values, int index) {
        if (index >= values.size()) return null;

        Integer value = values.get(index);
        if (value == null) return null;

        TreeNode tree = new TreeNode(value);

        // tree(index).left = 2 * index + 1
        tree.left = createTree(values, index * 2 + 1);

        // tree(index).right = 2 * index + 2
        tree.right = createTree(values, index * 2 + 2);

        return tree;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.