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:

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

Example 2:

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

Example 3:

  1. Input: root = [1,2,3,null,4], x = 2, y = 3
  2. 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.
  1. /*
  2. * @ desc: initBinaryTree
  3. * @ in :
  4. * aiArr
  5. * iSize
  6. * @ out :
  7. * target binary tree
  8. * @ cautious :
  9. * n pos in arr
  10. * child pos is 2n + 1, 2n + 2
  11. *
  12. * 0
  13. * 1 2
  14. * 3 4 5 6
  15. * ...
  16. * Child poionter is null that arr value is 0.
  17. * ele in aiArr is different.
  18. */
  19. struct TreeNode* initBinaryTree(int aiArr[], int iSize)
  20. {
  21. struct TreeNode *pstNode = NULL;
  22. struct TreeNode *pstLeft = NULL;
  23. struct TreeNode *pstRight = NULL;
  24. struct TreeNode *pstRoot = NULL;
  25. int i = ;
  26.  
  27. for (; i < iSize; i++)
  28. {
  29. if ( == i)
  30. {
  31. /* as root */
  32. if (!aiArr[]) return NULL;
  33. pstNode = malloc(sizeof(struct TreeNode));
  34. memset(pstNode, , sizeof(struct TreeNode));
  35. pstNode->val = aiArr[];
  36. pstNode->left = NULL;
  37. pstNode->right = NULL;
  38. pstRoot = pstNode;
  39. }
  40. else
  41. {
  42. if ( == aiArr[i])
  43. {
  44. continue;
  45. }
  46. pstNode = getNodeByVal(pstRoot, aiArr[i]);
  47. if (!pstNode)
  48. {
  49. return NULL;
  50. }
  51. }
  52.  
  53. /* construct child */
  54. if (iSize >= * (i + ) - )
  55. {
  56. if (aiArr[ * i + ])
  57. {
  58. pstLeft = malloc(sizeof(struct TreeNode));
  59. memset(pstLeft, , sizeof(struct TreeNode));
  60. pstLeft->val = aiArr[ * i + ];
  61. pstNode->left = pstLeft;
  62. }
  63.  
  64. if ((iSize >= * (i + ) ) && (aiArr[ * i + ]))
  65. {
  66. pstRight = malloc(sizeof(struct TreeNode));
  67. memset(pstRight, , sizeof(struct TreeNode));
  68. pstRight->val = aiArr[ * i + ];
  69. pstNode->right = pstRight;
  70. }
  71. }
  72. }
  73. return pstRoot;
  74. }
  75.  
  76. /*
  77. * @ desc: get binary tree height
  78. * @ in :
  79. * @ out :
  80. * @ ret :
  81. */
  82. int getBinTreeHeight(struct TreeNode* root)
  83. {
  84. int iLeftHeight = ;
  85. int iRightHeight = ;
  86.  
  87. if (!root) return ;
  88.  
  89. if (!root->left && !root->right) return ;
  90.  
  91. if (root->left)
  92. {
  93. iLeftHeight = getBinTreeHeight(root->left);
  94. }
  95.  
  96. if (root->right)
  97. {
  98. iRightHeight = getBinTreeHeight(root->right);
  99. }
  100. return (MAX_INT(iLeftHeight, iRightHeight) + );
  101. }
  102.  
  103. /*
  104. * @ desc: deserialization binary tree to arr
  105. * @ in :
  106. * root in_tree
  107. * @ out :
  108. * iSize arrSize
  109. * @ ret :
  110. * outPutArr
  111. */
  112. int* deserialBinTree(struct TreeNode* root, int* piSize)
  113. {
  114. int* piOut = NULL;
  115. int iHeight = ;
  116. int iSize = ;
  117. int i = ;
  118. int iStart = ;
  119. struct TreeNode* pstNode = NULL;
  120.  
  121. /* arrsize = 2 ^ iHeight - 1 */
  122. iHeight = getBinTreeHeight(root);
  123. if (iHeight > ) return NULL;
  124. iSize = (<<iHeight) - ;
  125.  
  126. piOut = malloc(sizeof(int) * iSize + );
  127. memset(piOut, , sizeof(int) * iSize + );
  128.  
  129. piOut[] = root->val;
  130.  
  131. for (i = ; i <= iSize; i++)
  132. {
  133. if (piOut[i])
  134. {
  135. pstNode = getNodeByVal(root, piOut[i]);
  136. if (!pstNode)
  137. {
  138. goto error;
  139. }
  140. /* set val in arr by index */
  141. if (pstNode->left)
  142. {
  143. piOut[i * + ] = pstNode->left->val;
  144. }
  145.  
  146. if (pstNode->right)
  147. {
  148. piOut[i * + ] = pstNode->right->val;
  149. }
  150. }
  151. }
  152.  
  153. *piSize = iSize;
  154. return piOut;
  155.  
  156. error:
  157. free(piOut);
  158. piOut = NULL;
  159. *piSize = ;
  160. return NULL;
  161. }
  162.  
  163. struct TreeNode* getBinNodeParent(struct TreeNode* root, int iVal)
  164. {
  165. struct TreeNode* pstNode = NULL;
  166. if (!root || (iVal == root->val))
  167. {
  168. return NULL;
  169. }
  170.  
  171. if (root->left)
  172. {
  173. if (iVal == root->left->val) return root;
  174. pstNode = getBinNodeParent(root->left, iVal);
  175. if (pstNode) return pstNode;
  176. pstNode = getBinNodeParent(root->right, iVal);
  177. if (pstNode) return pstNode;
  178. }
  179. if (root->right)
  180. {
  181. if (iVal == root->right->val) return root;
  182. pstNode = getBinNodeParent(root->left, iVal);
  183. if (pstNode) return pstNode;
  184. pstNode = getBinNodeParent(root->right, iVal);
  185. if (pstNode) return pstNode;
  186. }
  187. return pstNode;
  188. }
  189. int getBinNodeHeight(struct TreeNode* root, int iVal)
  190. {
  191. struct TreeNode* pstNode = NULL;
  192. int iHeight = ;
  193. if (root)
  194. {
  195. if (iVal == root->val) return iHeight;
  196. }
  197. if (root->left)
  198. {
  199. pstNode = getNodeByVal(root->left, iVal);
  200. if (pstNode) return (getBinNodeHeight(root->left, iVal) + );
  201. }
  202. if (root->right)
  203. {
  204. pstNode = getNodeByVal(root->right, iVal);
  205. if (pstNode) return (getBinNodeHeight(root->right, iVal) + );
  206. }
  207. return iHeight;
  208.  
  209. }
  210. struct TreeNode* getNodeByVal(struct TreeNode* root, int iVal)
  211. {
  212. struct TreeNode* pstNode = NULL;
  213. if (root)
  214. {
  215. if (iVal == root->val) return root;
  216. }
  217. if (root->left)
  218. {
  219. pstNode = getNodeByVal(root->left, iVal);
  220. if (pstNode) return pstNode;
  221. }
  222. if (root->right)
  223. {
  224. pstNode = getNodeByVal(root->right, iVal);
  225. if (pstNode) return pstNode;
  226. }
  227. return pstNode;
  228.  
  229. }
  230.  
  231. struct TreeNode* postTravel(struct TreeNode* root)
  232. {
  233. if (root->left) postTravel(root->left);
  234. if (root->right) postTravel(root->right);
  235. printf("%d ", root->val);
  236. return root;
  237. }
  238. struct TreeNode* getFistNodeByPostTravel(struct TreeNode* pstRoot)
  239. {
  240. return NULL;
  241. }
  242. struct TreeNode* getNextByPostTravel(struct TreeNode* pstRoot)
  243. {
  244. struct TreeNode* pstParentNode = NULL;
  245. if (pstRoot->left) return pstRoot->left;
  246. if (pstRoot->right) postTravel(pstRoot->right);
  247. pstParentNode = getBinNodeParent(pstRoot, pstRoot->val);
  248. if (!pstParentNode) return NULL;
  249. return getNextByPostTravel(pstParentNode);
  250. }
  251. #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. HDU 5191

    好端端的被HACK掉了...应该是在两端都要补W个0才对,之前只想到要在后面补足0,没想到前面也应该补足,因为前面即便存在0也可能使得移动的积木数最少.. T_T #include <iostr ...

  2. ruby for in 循环中改变i的值无效

    ruby for in 循环中改变i的值无效 for j in 1..5 puts "#{j}hehe" j = j + 2 #break end 在循环中,使用j = j + 2 ...

  3. java 源代码的魅力

    学习一种语言: 最快的方法.就是研究其源码. 从源码中可以体会到各种经典的思想! 赞赏一下: 比如: 我们在写一些 冒泡和选择排序的时候用的 交换:     /**      * Swaps x[a] ...

  4. Oracle 数据块损坏与恢复具体解释

    1.什么是块损坏: 所谓损坏的数据块,是指块没有採用可识别的 Oracle 格式,或者其内容在内部不一致. 通常情况下,损坏是由硬件故障或操作系统问题引起的.Oracle 数据库将损坏的块标识为&qu ...

  5. python spark 决策树 入门demo

    Refer to the DecisionTree Python docs and DecisionTreeModel Python docs for more details on the API. ...

  6. 使用filezella服务器安装ftp

    使用FileZilla配置FTP站点,可参考以下步骤: 1.打开Filezilla Server服务端: 点击[Edit]->[Users],或者点击如下图标新增用户. 2.添加FTP帐号后,设 ...

  7. position记录元素原始位置

    position记录元素原始位置 css样式: li { width: 100px; height: 100px; margin: 10px 0 0 10px; background-color: # ...

  8. Linux运维最佳实践之网站调优

    高性能静态网站: 1.静态页面中针对图片进行浏览器(客户端)缓存,如公共JavaScript(jQuery,jQuery-1.12.1.min.js)进行缓存 2.对网站输入内容压缩(gzip) 3. ...

  9. [hihocoder][Offer收割]编程练习赛43

    版本号排序 不知道什么傻逼原因,就是过不了 #pragma comment(linker, "/STACK:102400000,102400000") #include<st ...

  10. (转载)RxJava 与 Retrofit 结合的最佳实践

    RxJava 与 Retrofit 结合的最佳实践 作者:tough1985 感谢 DaoCloud 为作者提供的 500 RMB 写作赞助: 成为赞助方 /开始写作 前言 RxJava和Retrof ...