public TreeNode(int[] people) {
public int[][] reconstructQueue(int[][] people) {
int[][] res = new int[people.length][2];
if (people == null || people.length == 0) {
Arrays.sort(people, new Comparator<int[]>(){
public int compare(int[] a, int[] b) {
return a[0] == b[0] ? a[1] - b[1] : b[0] - a[0];
TreeNode root = new TreeNode(people[0]);
for (int i = 1; i < people.length; i++) {
insert(root, new TreeNode(people[i]), people[i][1]);
inorderTraverse(root, res);
public void insert(TreeNode root, TreeNode newNode, int k) {
while (curNode != null) {
if (k < curNode.leftCount) {
if (curNode.left == null) {
if (curNode.right == null) {
public void inorderTraverse(TreeNode root, int[][] res) {
Stack<TreeNode> stack = new Stack();
while (curNode != null || !stack.isEmpty()) {
res[index] = curNode.people;