leetCode笔记--binary tree
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.
* 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的更多相关文章
- leetcode 199 :Binary Tree Right Side View
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
- 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 ...
- (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
- 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- [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 ...
- LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)
翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...
- LeetCode—— Invert Binary Tree
LeetCode-- Invert Binary Tree Question invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 ...
- 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 ...
- LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)
翻译 给定一个二叉树,返回从下往上遍历经过的每一个节点的值. 从左往右,从叶子到节点. 比如: 给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7 返回它从下 ...
随机推荐
- js休眠
<!DOCTYPE html><html><meta http-equiv="Content-Type" content="text/htm ...
- webbench压力測试工具
apache的測试工具ab 在并发100个以上后会出现错误.网上也有非常多改ab源代码来解禁的. 只是还是推荐一款比較好用的压力測试工具webbench wget http://blog.zyan.c ...
- hdoj 3488 Tour 【最小费用最大流】【KM算法】
Tour Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total Submi ...
- 扩展欧几里德 poj1061 青蛙的约会
扩展欧几里德很经典.可是也有时候挺难用的.一些东西一下子想不明确.. 于是来了一个逆天模板..仅仅要能列出Ax+By=C.就能解出x>=bound的一组解了~ LL exgcd(LL a, LL ...
- Activiti的简单入门样例(经典的请假样例)
经典的请假样例: 流程例如以下,首先须要部门经理审批.假设请假天数大于2天,则须要总经理审批,否则HR审批就可以 一:创建maven项目,项目结构例如以下: watermark/2/text/aHR0 ...
- 在IIS6,7中部署ASP.NET网站
查看web.config文件 ASP.NET网站与一般的桌面程序不同,不是拷贝过来就能运行的(数据库连接除外). 要想运行它,通常需要一些配置过程.但是,我们到底需要配置什么呢?答案是:查看web.c ...
- [源码管理] Windows下搭建SVN服务器
前文所述SVN客户端使用的时候,用的SVN服务器通常为外部,例如Google Code的服务器,不过,做为一个程序开发人员,就算自己一个人写程序,也应该有一个SVN版本控制系统,以便对开发代码进行有效 ...
- NOIP2011 day2 第一题 计算系数
计算系数 NOIP2011 day2 第一题 描述 给定一个多项式(ax+by)^k,请求出多项式展开后x^n*y^m项的系数. 输入格式 共一行,包含5 个整数,分别为 a ,b ,k ,n ,m, ...
- LeetCode Weekly Contest 22
1. 532. K-diff Pairs in an Array 分析:由于要唯一,所以要去重,考虑k=0,时候,相同的数字需要个数大于1,所以,先用map统计个数,对于k=0,特判,对于其他的,遍历 ...
- (转)(C++)关于抽象基类和纯虚函数
★抽象类:一个类可以抽象出不同的对象来表达一个抽象的概念和通用的接口,这个类不能实例化(创造)对象. ★纯虚函数(pure virtual):在本类里不能有实现(描述功能),实现需要在子类中实现.例: ...