1ms Java Solution


  • 0
    P
    public class Solution {
        int result,j = 1;
        public int kthSmallest(TreeNode root, int k) {
            
            dfs(root,k);
            return result;
        }
        
        public void dfs(TreeNode root,int k){
            if(root==null)return;
            if(root.left!=null)dfs(root.left,k);
            if(j==k){result = root.val;j++;return;}
            else j++;
            if(root.right!=null)dfs(root.right,k);
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.