2020-10-23python实现二叉树的堂兄弟节点

python实现二叉树的堂兄弟节点

一采用递归法
利用哈希映射代码如下

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isCousins(self, root: TreeNode, x: int, y: int) -> bool:
        if not root:
            return False
        parent={}  #树的父节点
        depth={}   #树的深度
        def tree(root,par=None):
            if root:
                if par:
                    depth[root.val]=1+depth[par]  
                else:
                    depth[root.val]=1
                parent[root.val]=par
                tree(root.left,root.val)
                tree(root.right,root.val)
        tree(root)
        return depth[x]==depth[y] and parent[x]!=parent[y]

二采用迭代的方法
采用的是层次遍历的思想 ,如果两个节点在同一层并且父节点不一样的话返回True 否则返回False

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isCousins(self, root: TreeNode, x: int, y: int) -> bool:
        if not root:
            return False
    迭代
        ls=[root]
        while ls:
            li=[]
            ac=[]
            for _ in range(len(ls)):
                b=[]
                a=ls.pop()
                if a.right:
                    li.append(a.right)
                    ac.append(a.right.val)
                    b.append(a.right.val)
                if a.left:
                    li.append(a.left)
                    ac.append(a.left.val)
                    b.append(a.left.val)
                if b:
                    if x in b and y in b:
                        return False
            if x in ac and y in ac and (x not in b or y not in b):
                return True
            ls=li
        return False
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
l2-011 是杭电OJ上的一道题目,题目要求是给定一个二叉树的前序遍历和中序遍历结果,要求输出该二叉树的层次遍历结果。 下面是一个用 Python 实现的解法: ```python class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def buildTree(preorder, inorder): if not preorder or not inorder: return None root_val = preorder[0] root = TreeNode(root_val) root_index = inorder.index(root_val) left_inorder = inorder[:root_index] right_inorder = inorder[root_index+1:] left_preorder = preorder[1:1+len(left_inorder)] right_preorder = preorder[1+len(left_inorder):] root.left = buildTree(left_preorder, left_inorder) root.right = buildTree(right_preorder, right_inorder) return root def levelOrder(root): if not root: return [] result = [] queue = [root] while queue: level_vals = [] level_size = len(queue) for _ in range(level_size): node = queue.pop(0) level_vals.append(node.val) if node.left: queue.append(node.left) if node.right: queue.append(node.right) result.append(level_vals) return result # 测试 preorder = [1, 2, 4, 8, 9, 5, 3, 6, 7] inorder = [8, 4, 9, 2, 5, 1, 6, 3, 7] root = buildTree(preorder, inorder) result = levelOrder(root) print(result) ``` 这段代码首先定义了一个 `TreeNode` 类,用于表示二叉树节点。然后通过 `buildTree` 函数根据前序遍历和中序遍历结果构建出二叉树。最后使用 `levelOrder` 函数对构建好的二叉树进行层次遍历,并输出结果。 希望对你有帮助!如果有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值