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; } }
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)); } }