C# solution: inorder, DFS


  • 0
    B
    public class Solution 
    {
    	private int count;
    	private int result;
    	
        public int KthSmallest(TreeNode root, int k) 
    	{
    		count = k;
    
    		InOrder(root);
    
    		return result;
        }
    
    	private void InOrder(TreeNode root)
    	{
    		if (root == null) return;
    		InOrder(root.left);
    		count--;
    
    		if (count == 0) 
    		{
    			result = root.val;
    			return;
    		}
    		InOrder(root.right);
    	}
    }
    

Log in to reply
 

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