LeetCode 7 May, Cousins in Binary Tree, 判断二叉树的堂兄弟节点

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:

example 1

Input: root = [1, 2, 3, 4], x = 4, y = 3
Output: False

Example 2:

example 2

Input: root = [1, 2, 3, null, 4, null, 5], x = 5, y = 4

Output: True

Example 3:

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.

分析:

题目含义:给定一棵二叉树,二叉树的节点范围为2100,其中不包含重复的节点值。若给出两个节点值xy,判断xy是否是堂兄弟节点(节点深度相同但父节点不同)。若是返回True否则返回False

问题的本质是对二叉树进行搜索,并记录当前节点的值、父节点和深度。

涉及到二叉树的遍历,那么遍历方法多种多样:前序、中序、后续(这三种方法其遍历路径是相同的,本质为深度优先搜索(DFS)),层序(可以理解为广度优先搜索(BFS))。

不同的搜索方法有其自身的特性。这里不深入讨论。

方法一:

利用DFS方法遍历整棵树,由于每个节点的值都是唯一的,那么在遍历的同时可以创建一个字典,key为节点值,value存储该节点的parent、depth信息。当结束循环,在字典中查询xy的信息,判断后可以返回TrueorFalse.

Python3 代码如下:

# 方法一
# Runtime: 24ms
# Memory Usage: 13.8MB
class Solution:
    def isCousins(self, root: TreeNode, x: int, y: int) -> bool:
        self.nodeDic = {}
        self.dfs(root, None, 1)
        if self.nodeDic[x][1] == self.nodeDic[y][1] and self.nodeDic[x][0] != self.nodeDic[y][0]:
            return True
        else:
            return False
        
    def dfs(self, root, parent, depth):
        if not root:
            return 
        self.nodeDic[root.val] = (parent, depth)
        self.dfs(root.left, root, depth + 1)
        self.dfs(root.right, root, depth + 1)

# 方法二
# Runtime: ms
# Memory Usage: MB

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值