甜甜马里奥赛事总结第一天——Leetcode Weekly Contest 169 3562 / 5932

目前比赛停滞在一半左右的名词,每次只是两道题

但这次虽然也打了两道题却感觉吃力,最近荒废刷题的原因,真的是一日不刷自己知道,三日不刷,比赛知道

可以不打比赛,但是不可以不写题解。只有思考才能进步

题解正文:

第二题:

知识点:二叉搜索树是有序树,左侧节点比跟节点小,右侧大,这道排序题有天然优势

问题:如何获得二叉树的值,遍历总是有问题,因为比赛的时候我的遍历代码是试出来的,不知道为啥能成功

原因:

值传递 VS 引用传递:

区分不清导致

值传递只要变现是在内存中新开辟一块变量,给赋值操作的对象,更改新对象不会影响旧对象

引用传递对新值的更改会连同旧的一起,所以整个递归操作一直是对同一个list进行添加,因此最后直接输出即可不用再次赋值

 

改进点:或许可以同步遍历两颗树,遍历完排序完,而不是先存储后排序

改进点:

5296. All Elements in Two Binary Search Trees

Given two binary search trees root1 and root2.

Return a list containing all the integers from both trees sorted in ascending order.

 

Example 1:

甜甜马里奥赛事总结第一天——Leetcode Weekly Contest 169 3562 / 5932_第1张图片
Input: root1 = [2,1,4], root2 = [1,0,3]
Output: [0,1,1,2,3,4]

Example 2:

Input: root1 = [0,-10,10], root2 = [5,1,7,0,2]
Output: [-10,0,0,1,2,5,7,10]

Example 3:

Input: root1 = [], root2 = [5,1,7,0,2]
Output: [0,1,2,5,7]

Example 4:

Input: root1 = [0,-10,10], root2 = []
Output: [-10,0,10]

Example 5:

甜甜马里奥赛事总结第一天——Leetcode Weekly Contest 169 3562 / 5932_第2张图片
Input: root1 = [1,null,8], root2 = [8,1]
Output: [1,1,8,8]

 

Constraints:

  • Each tree has at most 5000 nodes.
  • Each node's value is between [-10^5, 10^5].

赛事代码:

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def getAllElements(self, root1, root2):
        """
        :type root1: TreeNode
        :type root2: TreeNode
        :rtype: List[int]
        """
        list1 = []
        list2 = []
        if root2==None:
            return self.getVal(root1, list1)
        if root1==None:
            return self.getVal(root2, list2)
        list1 = self.getVal(root1, list1)
        list2 = self.getVal(root2, list2)
        print(list1)
        print(list2)
        len_1 = len(list1)
        len_2 = len(list2)
        i, j = 0, 0
        ans = []
        while i != len_1 and j != len_2:
            if list1[i] <= list2[j]:
                ans.append(list1[i])
                i += 1
            else:
                ans.append(list2[j])
                j += 1
        if i != len_1:
            ans += list1[i:]
        else:
            ans += list2[j:]
        return ans

        print(list2)

    def getVal(self, root, res):
        if root.left:
            root3 = root.left
            res = self.getVal(root3, res)
        res.append(root.val)
        if root.right:
            root4 = root.right
            res = self.getVal(root4, res)
        return res

第三题:

5297. Jump Game III

知识点:BFS

问题:BFS不会写

拓展:

BFS:每次输出一行,所用数据结构为队列
设想我们每次都从左到右、从上到下的去遍历一个图,那么就需要把一行中最左边先进来的先输出,最右边后进来的后输出。所以会用到队列。

DFS:每次深挖到底,所用数据结构为栈
设想我们从图的最上边先按照一条道深挖到最下面,在挖到底以后就需要再逐个返回到上面的顶点,再去遍历父节点是不是还有别的子节点。后进先出的模式,所以需要用到栈。

def bfs(node):
    if node is None:
        return
    queue = []
    #nodeSet = set() 
    queue.insert(0,node)
    #nodeSet.add(node)
    while queue:
        cur = queue.pop()               # 弹出元素
        print(cur.val)                # 打印元素值
        for next in cur.nexts:          # 遍历元素的邻接节点
            #if next not in nodeSet:     # 若邻接节点没有入过队,加入队列并登记
                #nodeSet.add(next)
                queue.insert(0,next)
#nodeSet = set()
def dfs1(node):
    if node is None:
        return
    #nodeSet.add(node)
    print(node.val)
    #相当于树的前序遍历了,只不过这里把左右子节点放到了nexts的列表中
    for next in node.nexts:
        #if next not in nodeSet:
            dfs1(next)

问题:最后尝试写了下,没有成功,报错原因是循环次数超时



作者:小碧小琳
链接:https://www.jianshu.com/p/d2125448270b
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Given an array of non-negative integers arr, you are initially positioned at start index of the array. When you are at index i, you can jump to i + arr[i] or i - arr[i], check if you can reach to any index with value 0.

Notice that you can not jump outside of the array at any time.

Example 1:

Input: arr = [4,2,3,0,3,1,2], start = 5
Output: true
Explanation: 
All possible ways to reach at index 3 with value 0 are: 
index 5 -> index 4 -> index 1 -> index 3 
index 5 -> index 6 -> index 4 -> index 1 -> index 3 

Example 2:

Input: arr = [4,2,3,0,3,1,2], start = 0
Output: true 
Explanation: 
One possible way to reach at index 3 with value 0 is: 
index 0 -> index 4 -> index 1 -> index 3

Example 3:

Input: arr = [3,0,2,1,2], start = 2
Output: false
Explanation: There is no way to reach at index 1 with value 0. 

Constraints:

  • 1 <= arr.length <= 5 * 10^4
  • 0 <= arr[i] < arr.length
  • 0 <= start < arr.length

你可能感兴趣的:(甜甜马里奥赛事总结第一天——Leetcode Weekly Contest 169 3562 / 5932)