【leetcode】993. Cousins in Binary Tree
题目如下:
In a binary tree, the root node is at depth
0
, and children of each depthk
node are at depthk+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 valuesx
andy
of two different nodes in the tree.Return
true
if and only if the nodes corresponding to the valuesx
andy
are cousins.Example 1:
Input: root = [1,2,3,4], x = 4, y = 3
Output: falseExample 2:
Input: root = [1,2,3,null,4,null,5], x = 5, y = 4
Output: trueExample 3:
Input: root = [1,2,3,null,4], x = 2, y = 3
Output: falseNote:
- The number of nodes in the tree will be between
2
and100
.- Each node has a unique integer value from
1
to100
.
解题思路:遍历树,找到对应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的更多相关文章
- 【LeetCode】993. Cousins in Binary Tree 解题报告(C++ & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 【Leetcode_easy】993. Cousins in Binary Tree
problem 993. Cousins in Binary Tree 参考 1. Leetcode_easy_993. Cousins in Binary Tree; 完
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【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 ...
- 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)
这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...
- 【LeetCode】543. Diameter of Binary Tree 解题报告 (C++&Java&Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【leetcode】Minimum Depth of Binary Tree
题目简述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...
- 【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 ...
- 【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 ...
随机推荐
- centos6安装完成之后必要的配置
centos6安装完成之后必要的配置 一配置yum源 [root@centos61 ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirro ...
- Python3解leetcode Rotate Array
问题描述: Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: ...
- CocoaPods中pod search报错的解决办法
pod search报错的原因: 每次使用pod search命令的使用会在该目录下生成一个search_index.json文件,会逐渐的增大,文件达到一定大小后,ruby解析里面的json字符就会 ...
- 安装和使用Redis【转】
Redis是一个高性能的内存数据库,它体积轻巧性能又高,在企业中被广泛使用. 安装Redis Windows安装 Redis是为Linux系统设计的,但是也有团队为Windows做了移植.我们可以到这 ...
- [杂题]:C/c(二分答案)
题目传送门(内部题54) 输入格式 第一行一个整数表示$n$.第二行$n$个整数表示初始序列.(这行原题没有,是我加的)接下来$2n$行每行两个整数,分别表示$X_i,Y_i$.数据保证至少存在一种方 ...
- 如何让UIViewController自动弹出PickerView
因为响应者的一下属性inputView和inputAccessoryView都是只读的,所以如果想要指定弹出的view就要override 下面两个属性的get和set方法 UIResponder ( ...
- Linux:主设备号和次设备号
http://www.linuxidc.com/Linux/2011-03/33863.htm Linux的设备管理是和文件系统紧密结合的,各种设备都以文件的形式存放在/dev目录下,称为设备 ...
- Linux启动详细过程(开机启动顺序)
启动第一步--加载BIOS当你打开计算机电源,计算机会首先加载BIOS信息,BIOS信息是如此的重要,以至于计算机必须在最开始就找到它.这是因为BIOS中包含了CPU的相关信息.设备启动顺序信息.硬盘 ...
- 自动化测试常用断言的使用方法(python)-(转载@zhuquan0814
自动化测试中寻找元素并进行操作,如果在元素好找的情况下,相信大家都可以较熟练地编写用例脚本了,但光进行操作可能还不够,有时候也需要对预期结果进行判断. 这里介绍几个常用断言的使用方法,可以一定程度上帮 ...
- 距离矢量路由协议——RIP
距离矢量路由协议RIP: 众所周知,RIP(Routing Information Protocol),即路由信息协议,是一种距离矢量路由协议,它与IGRP,OSPF等一样都是属于IGP(Interi ...