C# code, log(h) space and O(n), concise code
public int KthSmallest(TreeNode root, int k) {\n                    // Olog(n) ~ O(h)\n            Stack<TreeNode> s = new Stack<TreeNode>();\n            while
    public int KthSmallest(TreeNode root, int k) {\n                    // Olog(n) ~ O(h)\n            Stack<TreeNode> s = new Stack<TreeNode>();\n            while (s.Count != 0 || root != null) {\n                if (root != null)\n                {\n                    s.Push(root);\n                    root = root.left;\n                }\n                else {\n                    root = s.Pop();\n                    k--; if (k == 0) break;\n                    root = root.right;\n                }\n            }\n\n            if (k > 0) throw new Exception();\n\n            return root.val;\n\n    }
[]