Created
October 21, 2014 16:59
-
-
Save augustt198/79f4fc62edb76d593999 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.august; | |
import java.util.Scanner; | |
/** | |
* Created by August on 10/21/2014. | |
*/ | |
public class EatingFrenzy { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
while (true) { | |
String[] parts = scanner.nextLine().split(" "); | |
int[] nums = new int[parts.length]; | |
for (int i = 0; i < parts.length; i++) { | |
nums[i] = Integer.parseInt(parts[i]); | |
} | |
printNode(tree(nums)); | |
} | |
} | |
private static Node tree(int[] arr) { | |
return tree(0, arr); | |
} | |
private static Node tree(int idx, int[] arr) { | |
Node node = new Node(); | |
node.value = arr[idx]; | |
int base = idx * 2; | |
if (base + 1 < arr.length) { | |
node.left = tree(base + 1, arr); | |
} | |
if (base + 2 < arr.length) { | |
node.right = tree(base + 2, arr); | |
} | |
return node; | |
} | |
private static void printNode(Node node) { | |
if (node.right != null) printNode(node.right); | |
if (node.left != null) printNode(node.left); | |
System.out.print(node.value + " "); | |
} | |
private static class Node { | |
int value; | |
Node left; | |
Node right; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment