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+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.
题目分析及思路
定义二叉树的深度:根结点的深度为0,深度为k的结点的孩子结点的深度为k+1。如果二叉树里的两个结点有相同的深度和不同的父结点,则称这两个结点为cousins。给定二叉树的根结点(每个结点的值唯一)以及两个不同结点的值,判断这两个结点是否是cousins。可以使用堆来存储结点,用字典parent和depth来存储各个结点的深度和父结点的值。遍历树的每一层,对该层结点的深度和父结点进行记录。
python代码
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isCousins(self, root: TreeNode, x: int, y: int) -> bool:
parent = {}
depth = {}
q = collections.deque()
q.append(root)
k = -1
while q:
k += 1
size = len(q)
for _ in range(size):
node = q.popleft()
if not node:
continue
depth[node.val] = k
q.append(node.left)
if node.left:
parent[node.left.val] = node.val
q.append(node.right)
if node.right:
parent[node.right.val] = node.val
return depth[x] == depth[y] and parent[x] != parent[y]
LeetCode 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(判断结点是否为Cousin)
993. Cousins in Binary Tree In a binary tree, the root node is at depth 0, and children of each dept ...
- 【LeetCode】965. Univalued Binary Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https://le ...
- 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【LeetCode】655. Print Binary Tree 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- LeetCode: Minimum Depth of Binary Tree 解题报告
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 【LeetCode】297. Serialize and Deserialize Binary Tree 解题报告(Python)
[LeetCode]297. Serialize and Deserialize Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode ...
- 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)
[LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
随机推荐
- Visual Studio 2017 - Windows应用程序打包成exe文件(2)- Advanced Installer 关于Newtonsoft.Json,LINQ to JSON的一个小demo mysql循环插入数据、生成随机数及CONCAT函数 .NET记录-获取外网IP以及判断该IP是属于网通还是电信 Guid的生成和数据修整(去除空格和小写字符)
Visual Studio 2017 - Windows应用程序打包成exe文件(2)- Advanced Installer Advanced Installer :Free for 30 da ...
- iOS性能优化篇 —— 耗电优化总结
手机App耗电的主要来源有以下四个因素: CPU处理,Processing 网络,Networking 定位,Location 图像,Graphics 耗电优化最终目的:通过尽可能降低CPU ...
- Android Glide 源码分析系列(待完成)
参考:https://jekton.github.io/2018/06/08/glide-disk-cache/ 参考:https://jekton.github.io/2018/06/20/glid ...
- Android内存泄漏杂谈
内存泄漏:是指内存得不到GC的及时回收,从而造成内存占用过多.从而导致程序Crash,也就是常说的OOM. 一.static 先来看以下一段代码 public class DBHelper { pri ...
- C#通过用户名与密码访问共享目录
C#通过用户名与密码访问共享目录 using System; using System.Collections.Generic; using System.Linq; using System.Tex ...
- LZW算法PHP实现方法 lzw_decompress php
LZW算法PHP实现方法 lzw_decompress php 博客分类: Php / Pear / Mysql / Node.js LZW算法简介 字符串和编码的对应关系是在压缩过程中动态生成的 ...
- Spark基本架构及原理
Hadoop 和 Spark 的关系 Spark 运算比 Hadoop 的 MapReduce 框架快的原因是因为 Hadoop 在一次 MapReduce 运算之后,会将数据的运算结果从内存写入到磁 ...
- SQL查看死锁+清理死锁
----查看sql死锁 CREATE procedure sp_who_lock as begin declare @spid int declare ...
- JavaScript数组删除指定元素
^_^ function arrayRemoveItem(arr, delVal) { if (arr instanceof Array) { var index = arr.indexOf(delV ...
- python调用shell脚本时需要切换目录
最近遇到了一个问题,就是python代码调用shell脚本时,发现输入输出的文件,总是和自己预想的有偏差,但是单独在linux下执行命令的时候,却没有错误.后来发现是相对路径的问题,因为执行pytho ...