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. HDOJ 题目5097 Page Rank(矩阵运算,模拟)

    Page Rank Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Tota ...

  2. HDU3117-Fibonacci Numbers(矩阵高速幂+log)

    题目链接 题意:斐波那契数列,当长度大于8时.要输出前四位和后四位 思路:后四位非常easy,矩阵高速幂取模,难度在于前四位的求解.  已知斐波那契数列的通项公式:f(n) = (1 / sqrt(5 ...

  3. oc17--点语法

    // // Person.h // day13 #import <Foundation/Foundation.h> @interface Person : NSObject { // @p ...

  4. sql server drop login failed

    https://stackoverflow.com/questions/37275/sql-query-for-logins https://www.mssqltips.com/sqlserverti ...

  5. hdoj--1068--Girls and Boys(最大独立集)

    Girls and Boys Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  6. FH Admin

    http://www.360doc.com/content/14/0713/08/8072791_394027312.shtml

  7. SQL注入原理解析以及举例1

    sql注入是指web应用程序对用户输入数据的合法性没有判断,导致攻击者可以构造不同的sql语句来实现对数据库的操作. sql注入漏洞产生满足条件: 1:用户能够控制数据的输入. 2:原本需要执行的代码 ...

  8. C# WinForm DataGridView 给标题列增加序号及格式化某个字段

    DataGridView 给标题列增加序号 private void dataGridView1_DataBindingComplete(object sender, DataGridViewBind ...

  9. Nginx 配置埋点js日志采集

    页面埋点&nginx日志采集 页面(web容器:httpd/nginx负载均衡 + apache server)<===> 日志采集服务器(nginx服务器) 通过某个页面跳转到我 ...

  10. Python基本数据类型之字符串str

    字符串 定义:它是一个有序的字符的集合,用于存储和表示基本的文本信息,‘’或“”或‘’‘ ’‘’中间包含的内容称之为字符串 字符串的结构类型为'...' "..." "' ...