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. # 从零開始搭建Hadoop2.7.1的分布式集群

    Hadoop 2.7.1 (2015-7-6更新),Hadoop的环境配置不是特别的复杂,可是确实有非常多细节须要注意.不然会造成很多配置错误的情况.尽量保证一次配置正确防止重复改动. 网上教程有非常 ...

  2. pagex,screenx,clientx的差别

    screenX:參照物为电脑的屏幕左上角,距离电脑屏幕的水平距离 clientX:參照物是内容区域左上角,距离内容区域左上角的水平距离,会随着滚动栏的移动而移动 pageX:參照物也是内容区域的左上角 ...

  3. 单片机project师必备的知识

    C语言编程 程序编译过程  哪部分干什么 数据结构  链表 二叉树 算法   数组  堆栈 队列算法 遍历 常量在内存中的表现形式 语法和内存的相应关系 数电 UML 类图 时序图 状态图 用例图   ...

  4. 2016.04.11,英语,《Vocabulary Builder》Unit 12

    umbr, from the Latin umbra, 'shadow', umbrella, -ella, 'little', means 'little shadow'. adumbrate: [ ...

  5. '无法将“vue”项识别为 cmdlet、函数、脚本文件或可运行程序的名称' 或 'vue不是内部或外部命令' 的解决方法

    如果在使用 vue 初始化项目的时候提示: vue : 无法将“vue”项识别为 cmdlet.函数.脚本文件或可运行程序的名称.请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次. 或者: ...

  6. NUnit Console Command Line

    https://github.com/nunit/docs/wiki/Console-Command-Line The console interface runner is invoked by a ...

  7. 通过top 5等待事件查看sql语句

    设计的动态性能视图有:v$session_event,v$session,v$sqlarea,首先在v$session_event中可以找到event,然后通过其动态性能视图找到sid,可以在v$se ...

  8. C++_class_powerpoint_1.1

    Types and Declarations Boolean Type bool type – boolean , logic type bool literal – true, falseint a ...

  9. php json 初始化函数(格式化json字符串为php json_decode 标准的字符串)

    $json="[{ 'i':100000, 'u':-1,n: '中国'},{i:100001,u:-1,n:'阿尔巴尼亚'},{i:100002,u:-1,n:'阿尔及利亚',}]&quo ...

  10. docker(三):Harbor 1.8.0 仓库的安装和使用

    回顾: docker(一):docker是什么? docker(二):CentOS安装docker docker(部署常见应用):docker部署mysql 安装的先决条件 硬件环境 1.CPU    ...