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:
- The number of nodes in the tree will be between
2
and100
. - Each node has a unique integer value from
1
to100
.
/*
* @ 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.
*/
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; int j = 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 = preTravel(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; } 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 = preTravel(root->left, iVal); if (pstNode) return (getBinNodeHeight(root->left, iVal) + 1); } if (root->right) { pstNode = preTravel(root->right, iVal); if (pstNode) return (getBinNodeHeight(root->right, iVal) + 1); } return iHeight; } struct TreeNode* preTravel(struct TreeNode* root, int iVal) { struct TreeNode* pstNode = NULL; if (root) { if (iVal == root->val) return root; } if (root->left) { pstNode = preTravel(root->left, iVal); if (pstNode) return pstNode; } if (root->right) { pstNode = preTravel(root->right, iVal); if (pstNode) return pstNode; } return pstNode; } bool isCousins(struct TreeNode* root, int x, int y) { int iHx = 0; int iHy = 0; struct TreeNode* px = NULL; struct TreeNode* py = NULL; iHx = getBinNodeHeight(root, x); iHy = getBinNodeHeight(root, y); if (iHx != iHy) return false; px = getBinNodeParent(root, x); py = getBinNodeParent(root, y); if (px == py) return false; return true; }
按数组结构构造binary树,当前写的是需要用到的。