Skip to content

Commit 1082534

Browse files
committed
0510 solved.
1 parent 4ee4b14 commit 1082534

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
project(cpp_0510)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(cpp_0510 main.cpp)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/// Source : https://leetcode.com/problems/inorder-successor-in-bst-ii/s
2+
/// Author : liuyubobobo
3+
/// Time : 2022-07-08
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
11+
/// Tree Algorithm
12+
/// Time Compelxity: O(h)
13+
/// Space Compelxity: O(1)
14+
15+
// Definition for a Node.
16+
class Node {
17+
public:
18+
int val;
19+
Node* left;
20+
Node* right;
21+
Node* parent;
22+
};
23+
24+
class Solution {
25+
public:
26+
Node* inorderSuccessor(Node* node) {
27+
28+
if(node->right){
29+
return minimum(node->right);
30+
}
31+
32+
Node* cur = node, *pre = node->parent;
33+
while(pre){
34+
if(pre->left == cur){
35+
return pre;
36+
}
37+
else{
38+
cur = pre;
39+
pre = cur->parent;
40+
}
41+
}
42+
return nullptr;
43+
}
44+
45+
private:
46+
Node* minimum(Node* node){
47+
while(node->left) node = node->left;
48+
return node;
49+
}
50+
};
51+
52+
53+
int main() {
54+
55+
return 0;
56+
}

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
539539
| 507 | [Perfect Number](https://leetcode.com/problems/perfect-number/) | [solution](https://leetcode.com/problems/perfect-number/solution/) | [C++](0501-1000/0507-Perfect-Number/cpp-0507/) | | |
540540
| 508 | [Most Frequent Subtree Sum](https://leetcode.com/problems/most-frequent-subtree-sum/) | [] | [C++](0501-1000/0508-Most-Frequent-Subtree-Sum/cpp-0508/) | | |
541541
| 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [] | [C++](0501-1000/0509-Fibonacci-Number/cpp-0509/) | | |
542-
| | | | | | |
542+
| 510 | [Inorder Successor in BST II](https://leetcode.com/problems/inorder-successor-in-bst-ii/) | [] | [C++](0501-1000/0510-Inorder-Successor-in-BST-II/cpp-0510/) | | |
543543
| 511 | Database Problem: [Link](https://github.com/liuyubobobo/Play-Leetcode-Database/) | - | - | - | - |
544544
| | | | | | |
545545
| 513 | [Find Bottom Left Tree Value](https://leetcode.com/problems/find-bottom-left-tree-value/) | [] | [C++](0501-1000/0513-Find-Bottom-Left-Tree-Value/cpp-0513/) | | |

0 commit comments

Comments
 (0)