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 ...
随机推荐
- js 判断浏览器是pc端还是移动端
if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) { //说明是移动端 } else { //说明是pc端 }
- while 格式化输出 运算符 字符编码
流程控制之while循环 条件循环:while,语法如下 while 条件: # 循环体 # 如果条件为真,那么循环体则执行,执行完毕后再次循环,重新判断条件... # 如果条件为假,那么循环体不执行 ...
- NOIP2017 PJ 跳房子 —— 单调队列优化DP
题目描述 跳房子,也叫跳飞机,是一种世界性的儿童游戏,也是中国民间传统的体育游戏之一.跳房子的游戏规则如下: 在地面上确定一个起点,然后在起点右侧画n个格子,这些格子都在同一条直线上.每个格子内有一个 ...
- 使用aliyun的oss服务器上传照片
1.控制台操作 首先介绍一下阿里云OSS对象存储的一些基本概念. 1.1 进入对象存储界面 登录阿里云账号,进入对象存储界面,如图所示. 进入后如图所示. 1.2 OSS基本概念 这里不过多介绍如何在 ...
- MySQL——时间戳和时间的转化
前言 Mysql中时间戳和时间的转化 时间转时间戳 select unix_timestamp('2019-7-29 14:23:25'); 时间戳转时间 select from_unixtime(1 ...
- centos7安装yum安装pip
pip是python中的一个包管理工具,可以对Python包的查找.下载.安装.卸载的作用. yum -y install epel-release yum -y install python-pip ...
- Ajax的个人总结
Ajax Ajax是Asynchronous Javascript And XML(异步JavaScript和XML)的缩写. Ajax技术描述了使用脚本操纵HTTP和Web服务器进行数据交换,在页面 ...
- S1_搭建分布式OpenStack集群_04 keystone认证服务安装配置
一.新建数据库及用户(控制节点)# mysql -uroot -p12345678MariaDB [(none)]> CREATE DATABASE keystone;MariaDB [(non ...
- WinDbg常用命令系列---||(系统状态)
||(系统状态) 简介 双竖线 ( || ) 命令将打印指定的系统或当前正在调试的所有系统的状态. 使用形式 || System 参数 System 指定要显示的系统. 如果省略此参数,将显示正在调试 ...
- C++函数声明后面加throw()的作用
原文地址:https://blog.csdn.net/to_baidu/article/details/53763683 C++里面为什么有时候在函数声明的时候在后面加throw()关键字? 解释: ...