package leetcode;

/**
 * https://leetcode.com/problems/kth-smallest-element-in-a-bst/
 */
public class Problem230 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    private static class Result {
        int n;
        int val;
        boolean found;
    }

    public int kthSmallest(TreeNode root, int k) {
        Result result = new Result();
        kthSmallest(root, k, result);
        return result.val;
    }

    private void kthSmallest(TreeNode node, int k, Result result) {
        if (result.n == k) {
            result.found = true;
            return;
        }
        if (node == null) {
            return;
        }
        kthSmallest(node.left, k, result);
        result.n++;
        if (!result.found) {
            result.val = node.val;
        }
        kthSmallest(node.right, k, result);
    }
}

@@ -278,104 +278,8 @@
  %7D%0A%0A
-    private static class Result %7B%0A        int n;%0A        int val;%0A        boolean found;%0A    %7D%0A%0A
     
@@ -337,34 +337,31 @@
     
-Result result
+IntRef ref
  = new 
-Result
+IntRef
 ();%0A
@@ -383,36 +383,33 @@
 lest(root, k, re
-sult
+f
 );%0A        retur
@@ -412,24 +412,23 @@
 eturn re
-sult
+f
 .val
+ue
 ;%0A    %7D%0A
@@ -444,155 +444,151 @@
 ate 
-void kthSmallest(TreeNode node, int k, Result result) %7B%0A        if (result.n == k) %7B%0A            result.found = true
+static class IntRef %7B%0A        private int value;%0A        private int n
 ;%0A    
+%7D%0A%0A
     
-    return;%0A        %7D
+private void kthSmallest(TreeNode node, int k, IntRef ref) %7B
 %0A   
@@ -674,28 +674,25 @@
 .left, k, re
-sult
+f
 );%0A        r
@@ -692,20 +692,17 @@
       re
-sult
+f
 .n++;%0A  
@@ -715,21 +715,18 @@
 if (
-!
 re
-sult.found
+f.n == k
 ) %7B%0A
@@ -743,16 +743,15 @@
   re
-sult
+f
 .val
+ue
  = n
@@ -755,24 +755,44 @@
 = node.val;%0A
+            return;%0A
         %7D%0A  
@@ -830,12 +830,9 @@
 , re
-sult
+f
 );%0A 

Update problem 230