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
2and100. - Each node has a unique integer value from
1to100.
/*
* @ 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 返回它从下 ...
随机推荐
- HDU 5402 Travelling Salesman Problem (模拟 有规律)(左上角到右下角路径权值最大,输出路径)
Travelling Salesman Problem Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (J ...
- 【待解决】创建maven web工程报错
报错信息如下: Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.6 o ...
- MySQL数据库表的数据插入、修改、删除、查询操作及实例应用
一.MySQL数据库表的数据插入.修改.删除和查询 CREATE DATABASE db0504; USE db0504; CREATE TABLE student ( sno ) NOT NULL ...
- 利用 Gearman 实现系统错误报警功能
Gearman 是什么? Gearman是一个用来把工作委派给其他机器.分布式的调用更适合做某项工作的机器.并发的做某项工作在多个调用间做负载均衡.或用来在调用其它语言的函数的系统. Gearman ...
- layer获取弹出frame层数据
通常,弹出层关闭之前,需要将部分数据传入父页面.这个时候怎么办呢? 通过success获取frame层的index. 然后通过cancel事件,获取子页面数据. 拿获取高德地图坐标为例: // 显示地 ...
- centos6.6--------正向DNS配置
一.正向区: 将域名解析为IP====================================================================================注 ...
- jquery.reveal弹出框
一款js弹出框,嵌入其它页面: 引用: <script src="../../js/jquery.reveal.js" type="text/javascript ...
- 很全很全的JavaScript的模块讲解
介绍 模块通常是指编程语言所提供的代码组织机制,利用此机制可将程序拆解为独立且通用的代码单元.所谓模块化主要是解决代码分割.作用域隔离.模块之间的依赖管理以及发布到生产环境时的自动化打包与处理等多个方 ...
- redis的基本命令
一.String类型的键值对 给一个变量赋值 set varName varVal eg 得到一个变量的值 get varName eg 删除一个变量 del varName eg del nume ...
- hdu4081 Qin Shi Huang's National Road System 次小生成树
先发发牢骚:图论500题上说这题是最小生成树+DFS,网上搜题解也有人这么做.但是其实就是次小生成树.次小生成树完全当模版题.其中有一个小细节没注意,导致我几个小时一直在找错.有了模版要会用模版,然后 ...