【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 ...
随机推荐
- Ubuntu下使用天河注意事项
把下载的.cgi登陆文件用文本编辑器打开,修改端口 用管理员权限打开.cgi才能绑定端口 $ gksu nautilus # browse files as root $ gksu gedit /et ...
- spring微服务(顺序由简入难易于理解)
一.为微服务应用增加健康监控 1.在 build.gradle 文件 dependencies 属性中增加 compile('org.springframework.boot:spring-boot- ...
- sql 连接的使用说明
SQL中的left outer join,inner join,right outer join用法详解 使用关系代数合并数据 关系代数 合并数据集合的理论基础是关系代数,它是由E.F.Codd于19 ...
- && 和 || 逻辑运算符的短路运算
&&和||的短路运算,是指如果在进行前面的表达式的运算过程,通过判断已经明确的知道整个表达式的结果,那么就不会进行后面表达式的运算判断. 表达式1 || 表达式2 || 表达式3... ...
- poj-1021--2D-Nim--点阵图同构
2D-Nim Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4136 Accepted: 1882 Descriptio ...
- 笔记:JFB 部署新环境,要更改的参数清单列表
ylbtech-笔记:JFB 部署新环境,要更改的参数清单列表 1. Web.config返回顶部 2. JS返回顶部 1./m/js/utils.js var utils = {} 序号 参数 ...
- Ubuntu安装 docker
安装docker首先要需要一台宿主机, 我目前用VMvare下安装的Ubuntu16.04系统为宿主机,进行docker安装测试. ubuntu安装时选的中文环境,生成的sources.list里面的 ...
- 大数据学习笔记之Hadoop(三):MapReduce&YARN
文章目录 一 MapReduce概念 1.1 为什么要MapReduce 1.2 MapReduce核心思想 1.3 MapReduce进程 1.4 MapReduce编程规范(八股文) 1.5 Ma ...
- laravel 中url使用
url() 通过url辅助函数(路由)生成:location.href = "{{url('user/index')}}"; 或者:location.href = "{{ ...
- git使用记录六:对commit的message做处理
修改最新commit的message git commit --amend 修改老旧commit的message 查询最新的三个log soaeon@DESKTOP-FUJJTHR MINGW64 / ...