简体   繁体   中英

Find the kth smallest node in binary search tree

class Solution {
    public int kthSmallest(TreeNode root, int k) {
        int cnt = 0;
        int val = -1000;
        int res  = inorderTraversal(root,k,cnt,val);
        return res;
    }


    public int inorderTraversal(TreeNode root,int k,int cnt,int val){

         if (root == null) 
            return val; 

        inorderTraversal(root.left,k,cnt,val);
        cnt++;
        //System.out.println(root.val);
        if(cnt == k){
            //System.out.println(root.val);
            val = root.val;
            return val;
        }
        inorderTraversal(root.right,k,cnt,val);
        return val;
    }
}

So I understood, I can use inorder traversal to find the kth smallest node. I am unable to understand, as soon as I find it, how do I send it back to the bottom stack of recursion. I am failing to get an answer here.

Note: This is not homework or any assignment. I am confused about recursion and trying to understand the approach

Questiun is from https://leetcode.com/problems/kth-smallest-element-in-a-bst/

Actually you could use a debugger to visualize stack frames as your recursion is executed.

Lets analyze it on a simple BST like:

      2

  1       3

Start at root = 2 ( TopStackFrame : { root = 2, k = 3, cnt = 0, val = -1000 } )

|

root != null

|

so goes to 1 ( TopStackFrame: { root = 1, k = 3, cnt = 0, val = -1000 } )

|

now goes to left of 1 ( TopStackFrame: { root = null, k = 3, cnt = 0, val = -1000 } )

|

root is null it returns val ( top of stack removed )

|

now come back to 1

|

cnt++ cnt becomes 1 ( TopStackFrame: { root = 1, k = 3, cnt = 1, val = -1000 } )

|

cnt != k goes to right of 1 ( TopStackFrame: { root = null, k = 3 cnt = 1, val = -1000 } )

|

right of 1 is null returns val ( top of stack removed )

|

then again return val ( TopStackFrame: { root = 1, k = 3, cnt = 1, val = -1000 } )

|

now 1 is done.

|

now you come to 2 ( TopStackFrame: { root = 2, k = 3, cnt = 0, val = -1000 } )

Now do you see that value of cnt is not 1 but 0 because each stackframe has its own set of variables.

Similar thing happens with the variable val .

There are several options to resolve this :

a) You could declare these as member variables in your class and update these in your recursion so you dont need to return anything with your recursion, just declare a method that doesn't return anything but updates these member variables.

b) You could use arrays like int[] val, int[] cnt with each one containing one element and update their elements in each recursive call.

c) You could use an iterative inorder traversal and get the value when count reaches k.

I would prefer 'c'. It is much cleaner, doesn't have to declare member variables or arrays. It also prevents stackoverflow exceptions in highly unbalanced trees.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM