LeetCode 993. Cousins in Binary Tree(判断结点是否为Cousin)
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
思路:
求解x,y的深度和父亲结点,如果深度一样,父亲结点不同,就是true;否则,就是false。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *getDepth(TreeNode* root, int val, int depth, int &level)
{
if (!root) return nullptr; //找到val的父亲,并设置val的depth
if ((root->left && root->left->val == val) || (root->right && root->right->val == val))
{
level = depth;
return root;
} TreeNode *left = getDepth(root->left, val, depth + , level);
if (left) return left; TreeNode *right = getDepth(root->right, val, depth + , level);
if (right) return right; return nullptr;
} bool isCousins(TreeNode *root, int x, int y)
{
int x_depth = -, y_depth = -;
TreeNode *x_parent = getDepth(root, x, , x_depth);
TreeNode *y_parent = getDepth(root, y, , y_depth);
if (x_depth == y_depth && x_parent != y_parent)
{
return true;
}
return false;
}
};
LeetCode 993. Cousins in Binary Tree(判断结点是否为Cousin)的更多相关文章
- LeetCode 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 ...
- 【Leetcode_easy】993. Cousins in Binary Tree
problem 993. Cousins in Binary Tree 参考 1. Leetcode_easy_993. Cousins in Binary Tree; 完
- 【LeetCode】993. Cousins in Binary Tree 解题报告(C++ & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 【leetcode】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 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化
Serialization is the process of converting a data structure or object into a sequence of bits so tha ...
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- [LeetCode] Serialize and Deserialize Binary Tree
Serialize and Deserialize Binary Tree Serialization is the process of converting a data structure or ...
- LeetCode——Serialize and Deserialize Binary Tree
Description: Serialization is the process of converting a data structure or object into a sequence o ...
随机推荐
- MySQL创建用户和加限权
目录 1.权限管理 1.1对新用户增删改 1.2对当前的用户授权管理 1.权限管理 我们知道我们的最高权限管理者是root用户,它拥有着最高的权限操作.包括select.update.delete ...
- Java入门(二)——泛型
如果你写过前端,可能会经常写一下关于变量类型的判断,比如:typeof fn === 'function'之类的代码.因为JavaScript作为一门弱类型语言,类型的判断往往需要开发人员自己去检查. ...
- 「资料分享」理解uboot要看哪些书
最开始是看的韦东山老师的视频,确实很不错,不过总感觉是不够深入扎实,还是想自己看看书,就总结搜罗下,以供参考 学习交流可以添加 微信读者交流①群 (添加微信:coderAllen) 程序员技术交流①群 ...
- HikariCP 个人实例
pom依赖 <!--HikariCP数据库连接池--> <dependency> <groupId>com.zaxxer</groupId> <a ...
- Hive UDF函数构建
1. 概述 UDF函数其实就是一个简单的函数,执行过程就是在Hive转换成MapReduce程序后,执行java方法,类似于像MapReduce执行过程中加入一个插件,方便扩展.UDF只能实现一进一出 ...
- wordpress非管理员看不到数据需有manage_options权限
今天ytkah在调试一个新功能的时候发现wordpress非管理员看不到一些插件的数据,比如editor,添加一些用户权限还是不行,不得已直接把administrator所有的权限都添加测试一遍,最后 ...
- docker容器中oracle数据库导出dmp文件
Oracle数据库安装在docker容器中 1首先查看容器 docker ps 2进入oracle容器 docker exec -it 7f0f3f2d4f88 /bin/bash 3导出整个库:这个 ...
- 通过三层交换机实现不同VLAN间的通信
主机的IP地址以及子网掩码已列出,下面将讲解如何配置利用三层交换机来实现不同VLAN间的相互通信 SW1的命令: en //进入特权模式 conf t //全局模式 vlan 10 // ...
- 【洛谷P3391】文艺平衡树——Splay学习笔记(二)
题目链接 Splay基础操作 \(Splay\)上的区间翻转 首先,这里的\(Splay\)维护的是一个序列的顺序,每个结点即为序列中的一个数,序列的顺序即为\(Splay\)的中序遍历 那么如何实现 ...
- javascript加密之md5加密
原作地址:JavaScriptMd5 修改备用:JavaScriptMd5.rar