@[TOC]20 day algorithm basic punch in

# Force deduction 20 day algorithm basic punch in

In order to deal with the interview of large factories, for the algorithm part, you need to punch in every day. Come on!

## No.1day breadth first search / depth first search

733. Image rendering

There is a picture represented by a two-dimensional integer array. Each integer represents the pixel value of the picture, and the value is between 0 and 65535.

Give you a coordinate (sr, sc) to represent the pixel value (row, column) at the beginning of image rendering and a new color value newColor to color the image again.

In order to complete the coloring work, start from the initial coordinates, record the connected pixels with the same pixel value as the initial coordinates in the upper, lower, left and right directions of the initial coordinates, and then record the qualified pixels in the four directions and their corresponding connected pixels with the same pixel value as the initial coordinates in the four directions,... Repeat the process. Change the color values of all recorded pixels to new color values.

Finally, the color rendered image is returned.

- Ideas and algorithms:

We start from a given starting point for depth first search. Each time a grid is searched, if it is the same color as the grid at the initial position, the color of the grid is updated to prevent repeated search; If not, backtracking is performed.

Note: because the color of the initial position will be modified, we need to save the color of the initial position for subsequent update operations. - code:

class Solution: def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]: n=len(image) m=len(image[0]) color=image[sr][sc] if color==newColor: return image def dfs(int p,int q): if image[p][q]==color: image[p][q]=newColor if p>=1: dfs(p-1,q) if p+1<n: dfs(p+1,q) if q>=1: dfs(p,q-1) if q+1<m: dfs(p,q+1) dfs(sr,sc) return image

617. Merge binary tree

Given two binary trees, imagine that when you overlay one of them on the other, some nodes of the two binary trees will overlap.

You need to merge them into a new binary tree. The merging rule is that if two nodes overlap, their values are added as the new value after node merging. Otherwise, the node that is not NULL will be directly used as the node of the new binary tree.

Source: LeetCode

Link: https://leetcode-cn.com/problems/merge-two-binary-trees

The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

- Ideas and algorithms:

We start from a given starting point for depth first search. There are three situations:

1. When one node of two trees is empty and one node is not empty, the combined node value is equal to the number that is not empty.

2. When both number nodes are not empty, the combined node value is equal to the sum of the node values of the two trees.

3. If the corresponding nodes of two binary trees are empty, the corresponding nodes of the merged binary tree are also empty; - code:

class Solution: def mergeTrees(self, root1: TreeNode, root2: TreeNode) -> TreeNode: if not root1: return root2 if not root2: return root1 merged=TreeNode(root1.val+root2.val) merged.left=self.mergeTrees(root1.left,root2.left) merged.right=self.mergeTrees(root1.right,root2.right) return merged

116. Traversing binary tree hierarchically

Populates the next right node pointer for each node. Given a perfect binary tree, all leaf nodes are in the same layer, and each parent node has two child nodes. Binary tree is defined as follows:

struct Node {

int val;

Node *left;

Node *right;

Node *next;

}

Fill in each of its next pointers so that this pointer points to its next right node. If the next right node cannot be found, set the next pointer to NULL.

In the initial state, all next pointers are set to NULL.

1. Code:

class Solution: def connect(self, root: 'Node') -> 'Node': if not root: return root #Create a new queue and insert the root node into the queue q=collections.deque([root]) while q: size=len(q) for i in range(size): #Output the head node of the queue node=q.popleft() if i<size-1: node.next=q[0] if node.left: q.append(node.left) if node.right: q.append(node.right) return root

21. Merge two ordered linked lists

Merge the two ascending linked lists into a new ascending linked list and return. The new linked list is composed of all nodes of a given two linked lists.

1. Code:

class Solution: def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: if not l1: return l2 if not l2: return l1 if l1.val<l2.val: l1.next=self.mergeTwoLists(l1.next,l2) return l1 else: l2.next=self.mergeTwoLists(l1,l2.next) return l2

88. Merge two ordered arrays

You are given two integer arrays nums1 and nums2 in non decreasing order, and two integers m and n representing the number of elements in nums1 and nums2 respectively.

Please merge nums2 into nums1 so that the merged array is also arranged in non decreasing order.

Note: finally, the merged array should not be returned by the function, but stored in the array nums1. In order to deal with this situation, the initial length of nums1 is m + n, where the first m elements represent the elements that should be merged, and the last n elements are 0, which should be ignored. The length of nums2 is n.

- code:

class Solution: def merge(self, nums1: List[int], m: int, nums2: List[int], n: int) -> None: """ Do not return anything, modify nums1 in-place instead. """ nums1[:]=sorted(nums1[:m]+nums2) return nums1

206 reverse linked list

Give you the head node of the single linked list. Please reverse the linked list and return the reversed linked list.

1. Code:

class Solution: def reverseList(self, head: ListNode) -> ListNode: prev=None#Create a new head node of the linked list cur=head#The head node of the original linked list while cur: next=cur.next#Record the next node of the head node of the original linked list cur.next=prev#Change the direction of the linked list prev=cur cur=next return prev