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)的更多相关文章

  1. 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 ...

  2. 【Leetcode_easy】993. Cousins in Binary Tree

    problem 993. Cousins in Binary Tree 参考 1. Leetcode_easy_993. Cousins in Binary Tree; 完

  3. 【LeetCode】993. Cousins in Binary Tree 解题报告(C++ & python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  4. 【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  ...

  5. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  6. [LeetCode] Serialize and Deserialize Binary Tree 二叉树的序列化和去序列化

    Serialization is the process of converting a data structure or object into a sequence of bits so tha ...

  7. 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 ...

  8. [LeetCode] Serialize and Deserialize Binary Tree

    Serialize and Deserialize Binary Tree Serialization is the process of converting a data structure or ...

  9. LeetCode——Serialize and Deserialize Binary Tree

    Description: Serialization is the process of converting a data structure or object into a sequence o ...

随机推荐

  1. 使用 chroot 建立沙盒环境

    使用 chroot 建立沙盒环境 chroot 提供了更改当前进程及其子进程的可见根目录的操作,运行在此隔离环境中的应用程序无法访问新的目录树之外的文件和命令.这样的隔离环境称作 chroot 监狱( ...

  2. 树莓派配置samba服务器,实现linux、windows文件共享

    一.安装samba服务器 输入如下命令: 二.配置文件smb.conf 找到[homes],将read only那里的yes改为no,允许读写 添加用户和设置密码 sudo smbpasswd -a ...

  3. php的插入排序

    感觉在这个数据量上,排入比冒泡要好很多呢~ 代码: <?php /** * 直接插入排序(类比抓牌) * 原理:每次从无序列表中取出第一个元素,把他插入到有序表中的合适位置,使有序表仍然有序 * ...

  4. 鸡尾酒排序Cocktail Sort (双向冒泡排序)

    鸡尾酒排序 鸡尾酒排序思路,先从左边开始进行冒泡排序,第一趟冒泡排序完,最大值在的数组的最右端,然后进行第二趟排序,第二趟排序从右边开始排序,第二趟结束时,最小值在数组最左端,以此类推,每一趟排序完都 ...

  5. Basic concepts of docker/kubernete/kata-container

    Kubereters An open-source system for automating deployment, scaling, and management of containerized ...

  6. K-Nearest Neighbors Algorithm

    K近邻算法. KNN算法非常简单,非常有效.KNN算法适合样本较少典型性较好的样本集. KNN的模型表示是整个训练数据集.也可以说是:KNN的特点是完全跟着数据走,没有数学模型可言. 对一个新的数据点 ...

  7. LeetCode 358. Rearrange String k Distance Apart

    原题链接在这里:https://leetcode.com/problems/rearrange-string-k-distance-apart/description/ 题目: Given a non ...

  8. S1_搭建分布式OpenStack集群_06 nova服务配置 (控制节点)

    一.创建数据库(控制节点)创建数据库以及用户:# mysql -uroot -p12345678MariaDB [(none)]> CREATE DATABASE nova_api;MariaD ...

  9. asp.net大附件上传,支持断点续传

    以ASP.NET Core WebAPI 作后端 API ,用 Vue 构建前端页面,用 Axios 从前端访问后端 API ,包括文件的上传和下载. 准备文件上传的API #region 文件上传  ...

  10. 分库分表相关 - hash与range结合去分库分表

    相关文章1 整体看下来通过hash取模去分库,然后根据range去分到哪个区间的表中. 具体还要实践下来.