TreeNode* KthNode(TreeNode* pRoot, unsigned int k)
 {
     if(pRoot)
     { 
         TreeNode *ret = KthNode(pRoot->left, k);
         if(ret) return ret;//为了可以逐层的返回
         if(++count == k) return pRoot;
         ret = KthNode(pRoot->right,k);
         if(ret) return ret;//为了可以逐层的返回
     }
     return NULL;
 }
Exemplo n.º 2
0
// ====================测试代码====================
void Test(const char* testName, const BinaryTreeNode* pRoot, unsigned int k, bool isNull, int expected)
{
    if(testName != nullptr)
        printf("%s begins: ", testName);

    const BinaryTreeNode* pTarget = KthNode(pRoot, k);
    if((isNull && pTarget == nullptr) || (!isNull && pTarget->m_nValue == expected))
        printf("Passed.\n");
    else
        printf("FAILED.\n");
}