leetcode day15 层序遍历 10 226.翻转二叉树 101.对称二叉树 2


提示:以下是本篇文章正文内容,下面案例可供参考

一、层序遍历

1-1.题目链接:102. 二叉树的层序遍历

link

2.独立做题问题总结

gif

3.解法总结:

  1. 队列
	def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
        s = deque()
        res = []
        size = 0
        if root == None:
            return []
        s.append(root)
        size = len(s)
        while s:
            store = []
            while size:
                cur = s.popleft()
                # print(cur.val)
                store.append(cur.val)
                if cur.left:
                    # print(cur.left.val)
                    s.append(cur.left)
                if cur.right:
                    # print(cur.right.val)
                    s.append(cur.right)
                size -= 1
            size = len(s)
            # print(f"size = {size}")
            res.append(store)
            # print(store)
        return res    
		

detail:
popleft(先入先出

1-2.题目链接:107. 二叉树的层序遍历 II

link

2.独立做题问题总结

python 反转list:1.a.reverse() 2.s=s[::-1]

3.解法总结:

		s = deque()
        res = []
        if root == None:
            return []
        s.append(root)
        size = 1
        while s:
            store = []
            while size:
                cur = s.popleft()
                store.append(cur.val)
                if cur.left:
                    s.append(cur.left)
                if cur.right:
                    s.append(cur.right)
                size -= 1
            size = len(s)
            res.append(store)
        res.reverse()    
        return res
		

1-3.题目链接:199.二叉树的右视图

link

2.独立做题问题总结

层序遍历的时候,判断是否遍历到单层的最后面的元素,如果是,就放进result数组中,随后返回result就可以了.

3.解法总结:

		s = deque()
        res = []
        if root == None:
            return []
        s.append(root)
        size = 1
        while s:
            
            while size:
                cur = s.popleft()
                if cur.left:
                    s.append(cur.left)
                if cur.right:
                    s.append(cur.right)
                size -= 1
            res.append(cur.val)
            size = len(s)
        return res

1-4.题目链接:637.二叉树的层平均值

link

2.独立做题问题总结

3.解法总结:

		s = deque()
        res = []
        if root == None:
            return root
        s.append(root)
        size = 1
        while s:
            store = []
            while size:
                cur = s.popleft()
                store.append(cur.val)
                if cur.left:
                    s.append(cur.left)
                if cur.right:
                    s.append(cur.right)
                size -= 1 
            size = len(s)
            res.append(mean(store))
        return res
		

1-5.题目链接:429.N叉树的层序遍历

link

2.独立做题问题总结

3.解法总结:

		s = deque()
        res = []
        if root == None:
            return []
        s.append(root)
        size = 1
        while s:
            store = []
            while size:
                cur = s.popleft()
                store.append(cur.val)
                if cur.children:
                    a = cur.children
                    while a:
                        s.append(cur.children.pop())
                size -= 1
            size = len(s)
            res.append(store[::-1])
        return res

1-6.题目链接:515.在每个树行中找最大值

link

2.独立做题问题总结

3.解法总结:

		s = deque()
        res = []
        if root == None:
            return []
        s.append(root)
        size = 1
        while s:
            store = []
            while size:
                cur = s.popleft()
                store.append(cur.val)
                if cur.left:
                    s.append(cur.left)
                if cur.right:
                    s.append(cur.right)
                size -= 1 
            size = len(s)
            res.append(max(store))
        return res

1-7.题目链接:116. 填充每个节点的下一个右侧节点指针

link

2.独立做题问题总结

3.解法总结:

		s = deque()
        size = 0
        if root == None:
            return root
        s.append(root)
        size = len(s)
        pre = None
        while s:
            while size:
                cur = s.popleft()
                if pre != None:
                    pre.next = cur
                if cur.left:
                    s.append(cur.left)
                if cur.right:
                    s.append(cur.right)
                size -= 1
                pre = cur
            cur.next = None
            pre = None
            size = len(s)
        return root    

1-8.题目链接:117. 填充每个节点的下一个右侧节点指针 II

link

2.独立做题问题总结

3.解法总结:

		s = deque()
        size = 0
        if root == None:
            return root
        s.append(root)
        size = len(s)
        pre = None
        while s:
            while size:
                cur = s.popleft()
                if pre != None:
                    pre.next = cur
                if cur.left:
                    s.append(cur.left)
                if cur.right:
                    s.append(cur.right)
                size -= 1
                pre = cur
            cur.next = None
            pre = None
            size = len(s)
        return root 

1-9.题目链接:104.二叉树的最大深度

link

2.独立做题问题总结

3.解法总结:

		s = deque()
        if root == None:
            return 0
        s.append(root)
        size = 1
        num = 0
        while s:
            while size:
                cur = s.popleft()
                if cur.left:
                    s.append(cur.left)        
                if cur.right:
                    s.append(cur.right)        
                size -= 1
            num += 1
            size = len(s)
        return num

1-10.题目链接:111.二叉树的最小深度

link

2.独立做题问题总结

3.解法总结:

		s = deque()
        if root == None:
            return 0
        s.append(root)
        size = 1
        num = 0
        while s:
            num += 1
            while size:
                cur = s.popleft()
                if cur.left:
                    s.append(cur.left)        
                if cur.right:
                    s.append(cur.right)     
                if cur.left == None and cur.right == None:
                    return num   
                size -= 1
            size = len(s)

二、leetcode 226.翻转二叉树

1.题目链接:

link

2.独立做题问题总结

一开始做的时候用的深度优先搜索

3.解法总结:

  1. 层序遍历,广度优先搜索
		s = deque()
        if root == None:
            return root
        s.append(root)
        size = 1
        while s:
            
            while size:
                cur = s.popleft()
                cur.left, cur.right = cur.right, cur.left
                if cur.left :
                    s.append(cur.left)
                if cur.right:
                    s.append(cur.right)
                size -= 1
            size = len(s)
        return root
		
  1. 递归(前序遍历,后序遍历)
		def helper(root):
            if root == None:
                return             
            helper(root.left)
            helper(root.right)
            root.left, root.right = root.right, root.left
        helper(root)
        return root

  1. 深度优先搜索
		s = deque()
        if root == None:
            return root
        s.append(root)
        while s:
            cur = s.popleft()
            if cur == None:
                continue
            cur.left, cur.right = cur.right, cur.left
            s.append(cur.left)
            s.append(cur.right)
        return root

三、leetcode 101.对称二叉树 2

1.题目链接:

100.相同的树
572.另一个树的子树

link

2.独立做题问题总结

最开始存入左右两个节点
循环内每次判断当前是否都为空,为空则继续,若一个为空一个不为空,或两者不相等,返回错误
存入新的节点。

3.解法总结:

  1. 广度优先搜索
		s = deque()
        if root == None:
            return True
        s.append(root.left)
        s.append(root.right)
        size = 2
        res = []
        while s:
            
            while size:
                cur1 = s.pop()
                cur2 = s.pop()
                if cur1 == None and cur2 == None:
                    size -= 2
                    continue
                if not cur1 or not cur2 or cur1.val != cur2.val:
                    return False
                s.append(cur1.left)
                s.append(cur2.right)
                s.append(cur1.right)
                s.append(cur2.left)
                size -= 2
            size = len(s)
        return True
		
  1. 递归
		def helper(root1, root2):
            if root1 == None and root2 == None:
                return True
            if root1 == None or root2 == None:
                return False
            if root1.val != root2.val:
                return False
            return helper(root1.left,root2.right) and helper(root1.right,root2.left)
        return helper(root.left, root.right)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值