# Sword finger offer08. Get the next node from the binary tree

This problem is not found in leetcode, but the analysis process is worth learning.

Pure situation discussion questions are required to be very detailed and complete (in fact, it is hard to think that qaq needs to draw a lot of diagrams for analysis):

- A node has a right subtree, and its next node is the leftmost node in the right subtree.
- No right subtree: if the node is a left child node, the next hop is its parent. If it is a right child node, keep looking for the parent until it becomes a member of the parent left tree

# Sword finger Offer 11. Rotate the minimum number of the array

Moving the first elements of an array to the end of the array is called array rotation. Enter a rotation of an incrementally sorted array and output the smallest element of the rotation array. For example, arrays

[3,4,5,1,2] is a rotation of [1,2,3,4,5], and the minimum value of the array is 1.

Partial order - > dichotomy

Num [mid] > num [right] the right half has a minimum value

Num [mid] < num [right] (there is a minimum value in the left half including mid...)

Otherwise – linear lookup

Note: be sure to compare with the value on the right. Although comparing with the value on the left can determine which interval is ordered, it is impossible to determine whether the smallest element is on the left or on the right.

Refer to leetcodeK's analysis:

Supplementary thinking: why not use dichotomy nums[m]nums[m] and nums[i]nums[i] For comparison? The purpose of dichotomy is to judge mm In which sort array to narrow the interval. And in nums[m] > nums[i]nums[m]>nums[i]In this case, it is impossible to judge mm In which sort array. Essentially because jj The initial value must be in the right sort array; ii The initial value cannot determine which sort array it is in. Examples are as follows: For the following two examples, when i = 0, j = 4, m = 2i=0,j=4,m=2 When, yes nums[m] > nums[i] ，The results are different. [1, 2, 3, 4 ,5][1,2,3,4,5] Rotation point x = 0x=0 : mm Sort the array on the right (this example has only the right sort array); [3, 4, 5, 1 ,2][3,4,5,1,2] Rotation point x = 3x=3 : mm Sort the array on the left.

So worst case O (n), average case O (log n)

class Solution { public: int minArray(vector<int>& numbers) { int i = 0, j = numbers.size() - 1; while (i < j) { int m = (i + j) / 2; if (numbers[m] > numbers[j]) i = m + 1; else if (numbers[m] < numbers[j]) j = m; else { //Direct linear search (or right -- instead of left + +) int x = i; for(int k = i + 1; k < j; k++) { if(numbers[k] < numbers[x]) x = k; } return numbers[x]; } } return numbers[i]; } };

# Sword finger Offer 12. Path in matrix

Give an m x n two-dimensional character grid board and a string word word. If word exists in the grid, return true; otherwise, return false.

Words must be formed alphabetically by letters in adjacent cells, where "adjacent" cells are those horizontally or vertically adjacent cells. Letters in the same cell are not allowed to be reused.

For example, in 3 below × The matrix of 4 contains the word "ABCCED" (the letters in the word are marked).

class Solution { public: int vis[201][201]={false}; int dx[4]={1,-1,0,0}; int dy[4]={0,0,1,-1}; bool res=false; bool exist(vector<vector<char>>& board, string word) { int n=board.size(),m=board[0].size(); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(board[i][j]==word[0]) { dfs(i,j,0,board,word); if(res) return res; } } } return false; } void dfs(int x,int y,int index,vector<vector<char>>& board, string word) { int n=board.size(),m=board[0].size(); if(index==word.length()||res) { res=true; return; } if(x>=0&&x<n&&y>=0&&y<m&&vis[x][y]==false&&board[x][y]==word[index]) { vis[x][y]=true; for(int i=0;i<4;i++) { int tx=x+dx[i]; int ty=y+dy[i]; dfs(tx,ty,index+1,board,word); } vis[x][y]=false; } else return; } };

Although I think it's very good, it's still only more than 5%.

If (index = = word. Length() |res) add conditions and prune, otherwise it will timeout...

# 13. Motion range of the robot

There is a grid with m rows and N columns on the ground, from coordinates [0,0] to coordinates [m-1,n-1]. A robot starts to move from the grid with coordinates [0,0], and it can move left, right, up and down one grid at a time (it cannot move outside the grid), nor can it enter the grid where the sum of the digits of row coordinates and column coordinates is greater than K. for example, when k is 18, the robot can enter the grid [35,37] , because 3 + 5 + 3 + 7 = 18. But it cannot enter the grid [35, 38], because 3 + 5 + 3 + 8 = 19. How many grids can the robot reach?

class Solution { public: int dx[2]={0,1}; int dy[2]={1,0}; int maxCount=0; bool vis[101][101]={false}; int movingCount(int m, int n, int k) { dfs(0,0,m,n,k); return maxCount; } void dfs(int x,int y,int m,int n,int k) { int numx=getCount(x); int numy=getCount(y); if(x<0||x>=m||y<0||y>=n||vis[x][y]==true||numx+numy>k) return; vis[x][y]=true; maxCount++; for(int i=0;i<2;i++) { int tx=x+dx[i]; int ty=y+dy[i]; dfs(tx,ty,m,n,k); } } int getCount(int n) { int s=0; while(n!=0) { s+=n%10; n/=10; } return s; } };

This question is not to find a path - so there is no need to backtrack. Has the vis array record counted the lattice