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 = ; for (; i < iSize; i++)
{
if ( == i)
{
/* as root */
if (!aiArr[]) return NULL;
pstNode = malloc(sizeof(struct TreeNode));
memset(pstNode, , sizeof(struct TreeNode));
pstNode->val = aiArr[];
pstNode->left = NULL;
pstNode->right = NULL;
pstRoot = pstNode;
}
else
{
if ( == aiArr[i])
{
continue;
}
pstNode = getNodeByVal(pstRoot, aiArr[i]);
if (!pstNode)
{
return NULL;
}
} /* construct child */
if (iSize >= * (i + ) - )
{
if (aiArr[ * i + ])
{
pstLeft = malloc(sizeof(struct TreeNode));
memset(pstLeft, , sizeof(struct TreeNode));
pstLeft->val = aiArr[ * i + ];
pstNode->left = pstLeft;
} if ((iSize >= * (i + ) ) && (aiArr[ * i + ]))
{
pstRight = malloc(sizeof(struct TreeNode));
memset(pstRight, , sizeof(struct TreeNode));
pstRight->val = aiArr[ * i + ];
pstNode->right = pstRight;
}
}
}
return pstRoot;
} /*
* @ desc: get binary tree height
* @ in :
* @ out :
* @ ret :
*/
int getBinTreeHeight(struct TreeNode* root)
{
int iLeftHeight = ;
int iRightHeight = ; if (!root) return ; if (!root->left && !root->right) return ; if (root->left)
{
iLeftHeight = getBinTreeHeight(root->left);
} if (root->right)
{
iRightHeight = getBinTreeHeight(root->right);
}
return (MAX_INT(iLeftHeight, iRightHeight) + );
} /*
* @ 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 = ;
int iSize = ;
int i = ;
int iStart = ;
struct TreeNode* pstNode = NULL; /* arrsize = 2 ^ iHeight - 1 */
iHeight = getBinTreeHeight(root);
if (iHeight > ) return NULL;
iSize = (<<iHeight) - ; piOut = malloc(sizeof(int) * iSize + );
memset(piOut, , sizeof(int) * iSize + ); piOut[] = root->val; for (i = ; 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 * + ] = pstNode->left->val;
} if (pstNode->right)
{
piOut[i * + ] = pstNode->right->val;
}
}
} *piSize = iSize;
return piOut; error:
free(piOut);
piOut = NULL;
*piSize = ;
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 = ;
if (root)
{
if (iVal == root->val) return iHeight;
}
if (root->left)
{
pstNode = getNodeByVal(root->left, iVal);
if (pstNode) return (getBinNodeHeight(root->left, iVal) + );
}
if (root->right)
{
pstNode = getNodeByVal(root->right, iVal);
if (pstNode) return (getBinNodeHeight(root->right, iVal) + );
}
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

leetCode笔记--binary tree的更多相关文章

  1. leetcode 199 :Binary Tree Right Side View

    // 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...

  2. LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal

    LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...

  3. (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...

  4. leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II

    leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  5. [LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

  6. LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)

    翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...

  7. LeetCode—— Invert Binary Tree

    LeetCode-- Invert Binary Tree Question invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 ...

  8. Java for LeetCode 107 Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  9. LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)

    翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...

随机推荐

  1. codevs——T2894 Txx考试

    http://codevs.cn/problem/2894/  时间限制: 1 s  空间限制: 32000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Descri ...

  2. 2015多校联合训练第一场Tricks Device(hdu5294)

    题意:给一个无向图,给起点s,终点t,求最少拆掉几条边使得s到不了t,最多拆几条边使得s能到t 思路: 先跑一边最短路,记录最短路中最短的边数.总边数-最短边数就是第二个答案 第一个答案就是在最短路里 ...

  3. swing Jlable中存放变量显示问题

    java swing 学习 在做一个ATM机系统小案例中.碰到JLable中存放变量,变量发生改变.而JLable中还是显示原来的值,网上寻找答案,用updateUI()和revalidate();方 ...

  4. HDU 5412 CRB and Queries(区间第K大 树套树 按值建树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5412 Problem Description There are N boys in CodeLan ...

  5. Linux命令(四)——文件权限管理

    文件权限是指对文件的访问控制,即哪些用户或群组可以访问文件以及执行什么样的操作. 一.文件的权限 1.Linux文件类型 (1)普通文件:文本文件+数据文件+可执行的二进制文件. (2)目录文件:即文 ...

  6. 2016.04.22,英语,《Vocabulary Builder》Unit 17

    anim, comes from the Latin anima, meaning 'breath' or 'soul'. animism: ['ænɪmɪzəm] n. 泛灵论,精神存在论,神创宇宙 ...

  7. 高可用技术工具包 High Availability Toolkit

    HighAvailabilityToolkit High Availability Toolkit includes several solutions by which achieving arch ...

  8. android监听虚拟按键的显示与隐藏【转】

    本文转载自:http://blog.csdn.net/u014583590/article/details/55263141 虚拟按键在华为手机中大量存在,而虚拟按键的存在无疑增加了屏幕适配的难度,往 ...

  9. [POJ 3621] Sightseeing Cows

    [题目链接] http://poj.org/problem?id=3621 [算法] 01分数规划(最优比率环) [代码] #include <algorithm> #include &l ...

  10. 微阅读,不依赖playground,打包成H5版本--案例学习

    微阅读,不依赖playground,打包成H5版本 https://github.com/vczero/weex-yy-h5