leecode

leecode637,二叉树的层平均值,bfs记录每一层然后取平均。

# 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 averageOfLevels(self, root: Optional[TreeNode]) -> List[float]:
        res = []
        level_node = []
        if root == None:
            return res
        level = 0
        q= deque([(root,0)])
        while len(q) != 0:
            node,height= q.popleft()
            if height == level:
                level_node.append(node.val)
            else:
                level += 1
                res.append(mean(level_node))
                level_node = []
                level_node.append(node.val)
            if node.left != None:
                q.append((node.left,height+1))
            if node.right != None:
                q.append((node.right,height+1))
        res.append(mean(level_node))
        return res

leecode429,N叉树的层序遍历,类似于二叉树的层序遍历

"""
# Definition for a Node.
class Node:
    def __init__(self, val=None, children=None):
        self.val = val
        self.children = children
"""


class Solution:
    def levelOrder(self, root: 'Node') -> List[List[int]]:
        res = []
        level_node = []
        if root == None:
            return res
        level = 0
        q= deque([(root,0)])
        while len(q) != 0:
            node,height= q.popleft()
            if height == level:
                level_node.append(node.val)
            else:
                level += 1
                res.append(level_node)
                level_node = []
                level_node.append(node.val)
            for child in node.children:
                q.append((child,height+1))
        res.append(level_node)
        return res

leecode993,二叉树的堂兄弟节点,bfs,注意终止条件

# 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: Optional[TreeNode], x: int, y: int) -> bool:
        res = []
        level_node = []
        if root == None:
            return res
        level = 0
        q= deque([(root,0)])
        height1 = 0
        height2 = 0
        while len(q) != 0:
            node,height= q.popleft()
            # print(node.val)
            if node.val == x:
                height1 = height
                print(x,height1)
            elif node.val == y:
                height2 = height
                print(y,height2)
            if height == level:
                level_node.append(node.val)
            else:
                level += 1
                res.append(level_node)
                level_node = []
                level_node.append(node.val)
            if node.left!=None and node.right!=None:
                if (node.left.val == x and  node.right.val == y) or (node.left.val == y and node.right.val == x):
                    print(node.left.val,node.right.val)
                    return False
            if node.left != None:
                q.append((node.left,height+1))
            if node.right != None:
                q.append((node.right,height+1))
                # print(height1,height2)
        return height1==height2

你可能感兴趣的:(leetcode,算法,职场和发展)