leetCode笔记--binary tree

993. Cousins in Binary Tree

In a binary tree, the root node is at depth 0, and children of each depth k node are at depth k+1.

Two nodes of a binary tree are cousins if they have the same depth, but have different parents.

We are given the root of a binary tree with unique values, and the values x and y of two different nodes in the tree.

Return true if and only if the nodes corresponding to the values x and y are cousins.

 

Example 1:

Input: root = [1,2,3,4], x = 4, y = 3
Output: false

Example 2:

Input: root = [1,2,3,null,4,null,5], x = 5, y = 4
Output: true

Example 3:

Input: root = [1,2,3,null,4], x = 2, y = 3
Output: false

 

Note:

  1. The number of nodes in the tree will be between 2 and 100.
  2. Each node has a unique integer value from 1 to 100.
/*
 * @ desc: initBinaryTree
 * @ in  :  
 *          aiArr 
 *          iSize
 * @ out :
 *         target binary tree
 * @ cautious :
 *  n pos in arr
 *  child pos is 2n + 1, 2n + 2
 *  
 *          0
 *      1       2
 *    3   4   5   6
 *  ...
 *  Child poionter is null that arr value is 0.
 *  ele in aiArr is different.
 */
struct TreeNode* initBinaryTree(int aiArr[], int iSize)
{
    struct TreeNode *pstNode            = NULL;
    struct TreeNode *pstLeft            = NULL;
    struct TreeNode *pstRight           = NULL;
    struct TreeNode *pstRoot            = NULL;
    int             i                   = 0;

    for (; i < iSize; i++)
    {
        if (0 == i)
        {
            /* as root */
            if (!aiArr[0]) return NULL;
            pstNode = malloc(sizeof(struct TreeNode));
            memset(pstNode, 0, sizeof(struct TreeNode));
            pstNode->val = aiArr[0];
            pstNode->left = NULL;
            pstNode->right = NULL;
            pstRoot = pstNode;
        }
        else
        {
            if (0 == aiArr[i])
            {
                continue;
            }
            pstNode =  getNodeByVal(pstRoot, aiArr[i]);
            if (!pstNode) 
            {
                return NULL;
            }
        }

        /* construct child */
        if (iSize >= 2 * (i + 1) - 1)
        {
            if (aiArr[2 * i + 1])
            {
                pstLeft = malloc(sizeof(struct TreeNode));
                memset(pstLeft, 0, sizeof(struct TreeNode));
                pstLeft->val = aiArr[2 * i + 1];
                pstNode->left = pstLeft;
            }

            if ((iSize >= 2 * (i + 1) ) && (aiArr[2 * i + 2]))
            {
                pstRight = malloc(sizeof(struct TreeNode));
                memset(pstRight, 0, sizeof(struct TreeNode));
                pstRight->val = aiArr[2 * i + 2];
                pstNode->right = pstRight;
            }
        }
    }
    return pstRoot;
}

/*
 * @ desc:  get binary tree height 
 * @ in  :
 * @ out :
 * @ ret :
 */
int getBinTreeHeight(struct TreeNode* root)
{
    int                 iLeftHeight         = 0;
    int                 iRightHeight        = 0;

    if (!root) return 0;

    if (!root->left && !root->right) return 1;

    if (root->left)
    {
        iLeftHeight = getBinTreeHeight(root->left);
    }

    if (root->right)
    {
        iRightHeight = getBinTreeHeight(root->right);
    }
    return (MAX_INT(iLeftHeight, iRightHeight) + 1);
}

/*
 * @ desc: deserialization binary tree to arr 
 * @ in  : 
 *          root        in_tree
 * @ out : 
 *          iSize       arrSize
 * @ ret : 
 *          outPutArr
 */
int* deserialBinTree(struct TreeNode* root, int* piSize)
{
    int*                piOut               = NULL;
    int                 iHeight             = 0;
    int                 iSize               = 0;
    int                 i                   = 0;
    int                 iStart              = 0;
    struct TreeNode*    pstNode             = NULL;
    
    /* arrsize = 2 ^ iHeight - 1 */
    iHeight = getBinTreeHeight(root);
    if (iHeight > 16) return NULL;
    iSize = (1<<iHeight) - 1;

    piOut = malloc(sizeof(int) * iSize + 1);
    memset(piOut, 0, sizeof(int) * iSize + 1);

    piOut[0] = root->val;

    for (i = 0; i <= iSize; i++)
    {
        if (piOut[i])
        {
            pstNode = getNodeByVal(root, piOut[i]);
            if (!pstNode) 
            {
                goto error;
            }
            /* set val in arr by index */
            if (pstNode->left)
            {
                piOut[i * 2 + 1] = pstNode->left->val;
            }

            if (pstNode->right)
            {
                piOut[i * 2 + 2] = pstNode->right->val;
            }
        }
    }
    
    *piSize = iSize;
    return piOut;
    
error:
    free(piOut); 
    piOut = NULL;
    *piSize = 0;
    return NULL;    
}

struct TreeNode* getBinNodeParent(struct TreeNode* root, int iVal)
{
    struct TreeNode*    pstNode             = NULL;
    if (!root || (iVal == root->val))
    {
        return NULL;
    }

    if (root->left)
    {
        if (iVal == root->left->val) return root;
        pstNode = getBinNodeParent(root->left, iVal);
        if (pstNode) return pstNode;
        pstNode = getBinNodeParent(root->right, iVal);
        if (pstNode) return pstNode;
    }
    if (root->right)
    {
        if (iVal == root->right->val) return root;
        pstNode = getBinNodeParent(root->left, iVal);
        if (pstNode) return pstNode;
        pstNode = getBinNodeParent(root->right, iVal);
        if (pstNode) return pstNode;
    }
    return pstNode;
}
int getBinNodeHeight(struct TreeNode* root, int iVal)
{
    struct TreeNode*    pstNode             = NULL;
    int                 iHeight             = 0;
    if (root)
    {
        if (iVal == root->val) return iHeight;
    }
    if (root->left)
    {
        pstNode = getNodeByVal(root->left, iVal);
        if (pstNode) return (getBinNodeHeight(root->left, iVal) + 1);
    }
    if (root->right)
    {
        pstNode = getNodeByVal(root->right, iVal);
        if (pstNode) return (getBinNodeHeight(root->right, iVal) + 1);
    }
    return iHeight;

}
struct TreeNode* getNodeByVal(struct TreeNode* root, int iVal)
{
    struct TreeNode*    pstNode             = NULL;
    if (root)
    {
        if (iVal == root->val) return root;
    }
    if (root->left)
    {
        pstNode = getNodeByVal(root->left, iVal);
        if (pstNode) return pstNode;
    }
    if (root->right)
    {
        pstNode = getNodeByVal(root->right, iVal);
        if (pstNode) return pstNode;
    }
    return pstNode;

}

struct TreeNode* postTravel(struct TreeNode* root)
{
    if (root->left) postTravel(root->left);
    if (root->right) postTravel(root->right);
    printf("%d ", root->val);
    return root;
}
struct TreeNode* getFistNodeByPostTravel(struct TreeNode* pstRoot)
{
    return NULL;
}
struct TreeNode* getNextByPostTravel(struct TreeNode* pstRoot)
{
    struct TreeNode*    pstParentNode   = NULL;
    if (pstRoot->left) return pstRoot->left;
    if (pstRoot->right) postTravel(pstRoot->right);
    pstParentNode = getBinNodeParent(pstRoot, pstRoot->val);
    if (!pstParentNode) return NULL;
    return getNextByPostTravel(pstParentNode);
}
#endif

 

posted @ 2019-03-21 17:51  ashen~  阅读(127)  评论(0编辑  收藏  举报