Iterative Java Solution Using Stack
Try to solve it iteratively, need an extra class to store:\n\n1. current TreeNode's coverage [low, up]\n\n2. current TreeNode entity\n\n()\n\n    public class S
Try to solve it iteratively, need an extra class to store:\n\n1. current TreeNode's coverage [low, up]\n\n2. current TreeNode entity\n\n()\n\n    public class Solution {\n        class Node{ // need another class to store multi information\n            int low, up; // means the TreeNode covers [low, up], low and up are all index\n            TreeNode t;\n            Node(int l, int p, TreeNode node){\n                low = l;\n                up = p;\n                t = node;\n            }\n        }\n        public TreeNode sortedArrayToBST(int[] num) {\n            if(num == null || num.length == 0) return null;\n            Stack<Node> stack = new Stack<Node>();\n            // initialize\n            TreeNode root = new TreeNode(num[(num.length-1)/2]);\n            Node rootNode = new Node(0,num.length-1,root);\n            stack.push(rootNode);\n            // iteration\n            while(!stack.isEmpty()){\n                Node node = stack.pop();\n                int middle = (node.low+node.up)/2; // cut half for [low, up]\n                \n                // [low, middle-1]\n                if(middle-1 >= node.low){\n                    TreeNode leftnode = new TreeNode(num[(middle-1+node.low)/2]);\n                    node.t.left = leftnode;\n                    Node left = new Node(node.low, middle-1, leftnode);\n                    stack.push(left);\n                }\n                // [middle+1, up]\n                if(middle+1 <= node.up){\n                    TreeNode rightnode = new TreeNode(num[(middle+1+node.up)/2]);\n                    node.t.right = rightnode;\n                    Node right = new Node(middle+1, node.up, rightnode);\n                    stack.push(right);\n                }\n            }\n            return root;\n        }\n    }
['Iterator', 'Java']