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 knode 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 yare 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 ...
随机推荐
- Docker-compose(创建容器)
Docker-compose(创建容器) 本文原始地址:https://sitoi.cn/posts/23955.html 样例 version: "2" services: sp ...
- BLE——协议层次结构
未完待续…… BLE协议 Bluetooth Application Applications GATT-Based Profiles/Services Bluetooth Core (Stack) ...
- Tensorflow简单实践系列(一):安装和运行
TensorFlow 是谷歌开发的机器学习框架. 安装 TensorFlow 直接使用 pip 安装即可,添加豆瓣镜像可以加快速度: pip install tensorflow -i https:/ ...
- 一道面试题关于js中逗号
一.今天遇到一个面试题,自我感觉是会,但是却做错了.人都是这样,自我感觉良好,其实也就预警自己已经忽视一些细节以及一些自我感知. 面试题: ,j=,k; ,j<;i++,j++){ k=i+j; ...
- python学习类与方法的调用规则
1类方法的特点是类方法不属于任何该类的对象,只属于类本身 2类的静态方法类似于全局函数,因为静态方法既没有实例方法的self参数也没有类方法的cls参数,谁都可以调用 3.实例方法只属于实例,是实例化 ...
- ajax、axios、fetch 对比
前言 今天在看到一个比较好的插件,写一个示例时,由于需要请求在线数据,官方给的是用 $.get(),就为了一个示例使用 JQuery 没必要. 又找了找,发现有用 fecth 的,挺方便,这里就做一个 ...
- Hive优化(整理版)
1. 概述 1.1 hive的特征: 可以通过SQL轻松访问数据的工具,从而实现数据仓库任务,如提取/转换/加载(ETL),报告和数据分析: 它可以使已经存储的数据结构化: 可以直接访问存储在Apac ...
- 1、Python简介与Python安装
一.Python简介: Python 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python的创始人为吉多·范罗苏姆(Guido van Rossum)少数几个不秃头的语言创始 ...
- c#语言学习笔记(1)
环境:VS Express 2013 for Desktop 也可以vs社区版,不过学习的话,Express本版做一些小的上位机工具应该是够用了 学习的网站:https://www.runoob.co ...
- BZOJ 1034: [ZJOI2008]泡泡堂BNB 贪心+排序
比较神奇的贪心 有点类似于田忌赛马. 如果我方最弱强于对面最弱,则直接最弱pk最弱. 如果我方最强强于对面最强,那么直接最强间pk. 否则,试着用我方最弱 pk 对方最强,看是否能打成平手. code ...