Share inorder c# 168ms solution


  • 0
    O

    public class Solution {

    public void Rank(TreeNode node, ref TreeNode kthNode, ref int counter, int k)
    {
      if (kthNode != null)
        return;
      if (node.left != null)
        Rank(node.left, ref kthNode, ref counter, k);
    
      if (kthNode != null)
        return;
    
      ++counter;
      if (counter == k)
      {
        kthNode = node;
        return;
      }
    
      if (node.right != null)
        Rank(node.right, ref kthNode,ref counter, k);
    }
    
    public int KthSmallest(TreeNode root, int k) {
      if (root == null)
        return -1;
    
      TreeNode kthNode = null;
      int counter = 0;
      Rank(root, ref kthNode, ref counter, k);
      if(kthNode != null)
      return kthNode.val;
    
      return -1;
    }
    

    }


Log in to reply
 

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