Trees

successors

feeling

  1. So the first thing you want to do is go through and find the target node P, and then you just have to take one more step
  2. But what happens next, if it’s the left node, it should go to its parent node; If it is the parent node, it should go to the right child node; If it’s a right child, it’s a left child; Three cases;
  3. However, maintaining the state of the current traversal of the subtree during DFS is a bit of a hassle, so the next approach is,
  4. Then I want to list all the nodes in the normal middle order. In the middle order push node, check whether it is the target node, save the current index, and find the node after traversing the entire tree.
  5. Instead of maintaining the entire array, maintain a FINISHED state
/ / https://leetcode-cn.com/problems/successor-lcci/ / * 1 * * * @ analysis. 2. Check if it is a target node, and record the current pos * 3. 4. If the array is exceeded, then there is no next node. Succeeded = function(root, succeeded)$O(n)$/ var Inorderantecedent = function(root, succeeded)$O(n)$$ p) { const res = [] let pos = 0 let index = 0 const dfs = (root) => { if(! root) return dfs(root.left) res.push(root) if(root === p){ pos = index } index++ dfs(root.right) } dfs(root) return res[pos+1] || null }; var inorderSuccessor2 = function(root, p) { let isFinished = false let res = null const dfs = (root) => { if(! DFS (root.left) if(isFinished){res = root IsFinished = false return} if(root === p){ IsFinished = true} DFS (root.right)} DFS (root) return res}; isFinished = true} DFS (root) return res};Copy the code

91 algorithm

Binary search

Find the peak

// https://leetcode-cn.com/problems/find-peak-element/ /** * @ The element whose peak value is greater than the left and right adjacent values * 2. Set the two security guards to negative infinity to ensure that the corresponding value * 3 can be obtained. */ var findPeakElement = function (nums) {let res = undefined nums[-1] = nums[nums.length] = -Infinity const recursion = (left, right) => { if (res || left > right) return const mid = left+Math.floor((right-left)/2) if (nums[mid] > nums[mid - 1] && Nums [mid] > nums[mid + 1]) {res = mid return} if (nums[mid] < nums[mid-1] && nums[mid] < nums[mid + 1]) {// Recursion (left, mid) recursion(mid + 1, right) else if (nums[mid] > nums[mid]) { (mid + 1, right)} else {// Recursion (0, nums.length-1) if(res){return nums[res]>nums[nums.length-1]? res:nums.length-1 } return res };Copy the code

instructions

I should have sent it last night, but when I was exercising last night, my computer was on until it ran out of power and shut down. Emm, when I come home from work, I usually take my computer home with a full charge. I wonder if it has any power. I can’t dig gold anymore. So I can only use my phone charger full, send one in the morning, this was actually written yesterday morning.

91 will come to the end of this month, basically morning time is algorithm, the order is daily problem (my own thematic brush, now tree), then 91, if there is still time to brush LC daily problem; Strive for 2021 end can take a mouse pad; Come on, duck, cook.