题目如下:

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:

  1. The number of nodes in the tree will be between 2 and 100.
  2. Each node has a unique integer value from 1 to 100.

解题思路:遍历树,找到对应X和Y的节点,并记录其level和parent即可。

代码如下:

# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
x_p = None
y_p = None
x_l = 0
y_l = 0
def recursive(self,node,level,parent,x,y):
if node.val == x:
self.x_p = parent
self.x_l = level
elif node.val == y:
self.y_p = parent
self.y_l = level
if node.left != None:
self.recursive(node.left,level+1,node,x,y)
if node.right != None:
self.recursive(node.right, level + 1, node, x, y)
def isCousins(self, root, x, y):
"""
:type root: TreeNode
:type x: int
:type y: int
:rtype: bool
"""
self.x_p = None
self.y_p = None
self.x_l = 0
self.y_l = 0
self.recursive(root,0,None,x,y)
return self.x_p != self.y_p and self.x_l == self.y_l

【leetcode】993. Cousins in Binary Tree的更多相关文章

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

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

  2. 【Leetcode_easy】993. Cousins in Binary Tree

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

  3. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  4. 【LeetCode】105 & 106. Construct Binary Tree from Inorder and Postorder Traversal

    题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume ...

  5. 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)

    这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...

  6. 【LeetCode】543. Diameter of Binary Tree 解题报告 (C++&Java&Python)

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

  7. 【leetcode】Minimum Depth of Binary Tree

    题目简述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...

  8. 【leetcode】Minimum Depth of Binary Tree (easy)

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  9. 【LeetCode】105 & 106 Construct Binary Tree from (Preorder and Inorder) || (Inorder and Postorder)Traversal

    Description: Given arrays recording 'Preorder and Inorder' Traversal (Problem 105) or  'Inorder and ...

随机推荐

  1. 【leetcode】828. Unique Letter String

    题目如下: A character is unique in string S if it occurs exactly once in it. For example, in string S = ...

  2. linux学习-常用文本处理命令

    1.文本处理命令 (1) tr 转换或删除字符 tr [OPTION]...SET1 SET2 选项: -c 取SET1字符串的补集 -d 删除属于SET1中的字符 -s 把连续重复出现的字符以单独一 ...

  3. HTTP请求流程基础知识

    HTTP协议解析: HTTP即超文本传输协议,是一种详细规定了浏览器和万维网服务器之间互相通信的规则,它是万维网交换信息的基础,它允许将HTML文档从WEB服务器传输到WEB浏览器. URL(统一资源 ...

  4. web uploader 上传大文件总结

    这里只写后端的代码,基本的思想就是,前端将文件分片,然后每次访问上传接口的时候,向后端传入参数:当前为第几块文件,和分片总数 下面直接贴代码吧,一些难懂的我大部分都加上注释了: 上传文件实体类: 看得 ...

  5. 如何稀释 流事件 (如,onscroll、change、input、mouseover 等 事件)

    1.问题引入:https://segmentfault.com/q/1010000000707337?_ea=62905 2.javascript中的函数节流和函数去抖:https://www.cnb ...

  6. [Repost] 常用素数

    作者:Miskcoo(http://blog.miskcoo.com/2014/07/fft-prime-table) 如果 \(r\cdot 2^k+1\) 是个素数, 那么在 \(\bmod r\ ...

  7. 重温HTML和CSS3

    重温Web前端基础 本篇幅中着重文字,只是记录一些自己的见解,巩固下自身基础 网页结构是什么? 结构层 html 导航,列表,段文字,图片,链接,表示层 css 颜色,大小,位置,行为层 JavaSc ...

  8. .net core linux的守护进程 supervisor

    这个介绍的很全面,对初学者来说可以有更好的认识: https://www.cnblogs.com/savorboard/p/dotnetcore-supervisor.html

  9. Class-DbConnectionManipulator:Execute,QueryFirstDefault<>

    ylbtech-Class-DbConnectionManipulator:Execute,QueryFirstDefault<> 1.返回顶部 1. public object GetS ...

  10. Windows-计划任务-自动备份数据库和文件

    开始 -> 程序 -> 附件 -> 系统工具 -> 计划任务 .bat 文件如下: ::数据库+文件备份 @echo off ::日期时间 set yyyymmdd=%date ...