Esempio n. 1
0
 public AumentedTree(TreeNode node)
 {
     if (node != null)
     {
         this.Node = node;
         Left      = new AumentedTree(node.left);
         Right     = new AumentedTree(node.right);
         Count     = 1 + Left.Count + Right.Count;
     }
 }
Esempio n. 2
0
 private int KthSmallest(AumentedTree aumentedTree, int countPreviousSmaller)
 {
     if (aumentedTree == null)
     {
         return(0);
     }
     if (aumentedTree.Left.Count + countPreviousSmaller + 1 == k)
     {
         return(aumentedTree.Node.val);
     }
     if (aumentedTree.Left.Count + countPreviousSmaller >= k)
     {
         return(KthSmallest(aumentedTree.Left, countPreviousSmaller));
     }
     else
     {
         return(KthSmallest(aumentedTree.Right, countPreviousSmaller + aumentedTree.Left.Count + 1));
     }
 }