Day61力扣打卡

打卡记录

Day61力扣打卡_第1张图片


反转二叉树的奇数层(bfs)

链接

class Solution:
    def reverseOddLevels(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
        q = [root]
        flag = 0
        while q:
            t, q = q, []
            if flag:
                l, r = 0, len(t) - 1
                while l < r:
                    t[l].val, t[r].val = t[r].val, t[l].val
                    l += 1
                    r -= 1
            flag ^= 1
            for node in t:
                if node.left:
                    q.append(node.left)
                if node.right:
                    q.append(node.right)
        return root

你可能感兴趣的:(leetcode刷题打卡,leetcode,算法,职场和发展,python)