diff --git a/lcci/04.06.Successor/README.md b/lcci/04.06.Successor/README.md index 738d9e84b6456..e23edb9b80530 100644 --- a/lcci/04.06.Successor/README.md +++ b/lcci/04.06.Successor/README.md @@ -35,7 +35,18 @@ ## 解法 -### 方法一 +### 方法一:二分搜索 + +二叉搜索树的中序遍历是一个升序序列,因此可以使用二分搜索的方法。 + +二叉搜索树节点 $p$ 的中序后继节点满足: + +1. 中序后继的节点值大于 $p$ 的节点值 +2. 中序后继是所有大于 $p$ 的节点中值最小的节点 + +因此,对于当前节点 $root$,如果 $root.val \gt p.val$,则 $root$ 可能是 $p$ 的中序后继节点,将 $root$ 记为 $ans$,然后搜索左子树,即 $root = root.left$;如果 $root.val \leq p.val$,则 $root$ 不能是 $p$ 的中序后继节点,搜索右子树,即 $root = root.right$。 + +时间复杂度 $O(h)$,其中 $h$ 为二叉搜索树的高度。空间复杂度 $O(1)$。 @@ -49,19 +60,14 @@ class Solution: - def inorderSuccessor(self, root: TreeNode, p: TreeNode) -> TreeNode: - def dfs(root): - if root is None: - return - dfs(root.left) - nonlocal ans, prev - if prev == p: + def inorderSuccessor(self, root: TreeNode, p: TreeNode) -> Optional[TreeNode]: + ans = None + while root: + if root.val > p.val: ans = root - prev = root - dfs(root.right) - - ans = prev = None - dfs(root) + root = root.left + else: + root = root.right return ans ``` @@ -76,28 +82,17 @@ class Solution: * } */ class Solution { - private TreeNode prev; - private TreeNode p; - private TreeNode ans; - public TreeNode inorderSuccessor(TreeNode root, TreeNode p) { - prev = null; - ans = null; - this.p = p; - dfs(root); - return ans; - } - - private void dfs(TreeNode root) { - if (root == null) { - return; - } - dfs(root.left); - if (prev == p) { - ans = root; + TreeNode ans = null; + while (root != null) { + if (root.val > p.val) { + ans = root; + root = root.left; + } else { + root = root.right; + } } - prev = root; - dfs(root.right); + return ans; } } ``` @@ -114,23 +109,18 @@ class Solution { */ class Solution { public: - TreeNode* prev; - TreeNode* p; - TreeNode* ans; - TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) { - this->p = p; - dfs(root); + TreeNode* ans = nullptr; + while (root) { + if (root->val > p->val) { + ans = root; + root = root->left; + } else { + root = root->right; + } + } return ans; } - - void dfs(TreeNode* root) { - if (!root) return; - dfs(root->left); - if (prev == p) ans = root; - prev = root; - dfs(root->right); - } }; ``` @@ -143,91 +133,46 @@ public: * Right *TreeNode * } */ -func inorderSuccessor(root *TreeNode, p *TreeNode) *TreeNode { - var prev, ans *TreeNode - var dfs func(root *TreeNode) - dfs = func(root *TreeNode) { - if root == nil { - return - } - dfs(root.Left) - if prev == p { +func inorderSuccessor(root *TreeNode, p *TreeNode) (ans *TreeNode) { + for root != nil { + if root.Val > p.Val { ans = root + root = root.Left + } else { + root = root.Right } - prev = root - dfs(root.Right) } - dfs(root) - return ans + return } ``` -```js +```ts /** * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } * } */ -/** - * @param {TreeNode} root - * @param {TreeNode} p - * @return {TreeNode} - */ -var inorderSuccessor = function (root, p) { - if (root == null) { - return root; - } - const { val, left, right } = root; - const res = inorderSuccessor(left, p); - if (res != null) { - return res; - } - if (val > p.val) { - return root; - } - return inorderSuccessor(right, p); -}; -``` - - - -### 方法二 - - -```cpp -/** - * Definition for a binary tree node. - * struct TreeNode { - * int val; - * TreeNode *left; - * TreeNode *right; - * TreeNode(int x) : val(x), left(NULL), right(NULL) {} - * }; - */ -class Solution { -public: - TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) { - stack stk; - TreeNode* cur = root; - while (cur != nullptr || !stk.empty()) { - if (cur == nullptr) { - cur = stk.top(); - stk.pop(); - if (cur->val > p->val) { - return cur; - } - cur = cur->right; - } else { - stk.push(cur); - cur = cur->left; - } +function inorderSuccessor(root: TreeNode | null, p: TreeNode | null): TreeNode | null { + let ans: TreeNode | null = null; + while (root) { + if (root.val > p.val) { + ans = root; + root = root.left; + } else { + root = root.right; } - return cur; } -}; + return ans; +} ``` ```js @@ -244,21 +189,16 @@ public: * @return {TreeNode} */ var inorderSuccessor = function (root, p) { - const stack = []; - let cur = root; - while (cur != null || stack.length !== 0) { - if (cur == null) { - cur = stack.pop(); - if (cur.val > p.val) { - return cur; - } - cur = cur.right; + let ans = null; + while (root) { + if (root.val > p.val) { + ans = root; + root = root.left; } else { - stack.push(cur); - cur = cur.left; + root = root.right; } } - return cur; + return ans; }; ``` diff --git a/lcci/04.06.Successor/README_EN.md b/lcci/04.06.Successor/README_EN.md index 3bba52a9e2c87..6934f70505df2 100644 --- a/lcci/04.06.Successor/README_EN.md +++ b/lcci/04.06.Successor/README_EN.md @@ -54,7 +54,18 @@ ## Solutions -### Solution 1 +### Solution 1: Binary Search + +The in-order traversal of a binary search tree is an ascending sequence, so we can use the binary search method. + +The in-order successor node of a binary search tree node $p$ satisfies: + +1. The value of the in-order successor node is greater than the value of node $p$. +2. The in-order successor is the node with the smallest value among all nodes greater than $p$. + +Therefore, for the current node $root$, if $root.val > p.val$, then $root$ could be the in-order successor of $p$. We record $root$ as $ans$ and then search the left subtree, i.e., $root = root.left$. If $root.val \leq p.val$, then $root$ cannot be the in-order successor of $p$, and we search the right subtree, i.e., $root = root.right$. + +The time complexity is $O(h)$, where $h$ is the height of the binary search tree. The space complexity is $O(1)$. @@ -68,19 +79,14 @@ class Solution: - def inorderSuccessor(self, root: TreeNode, p: TreeNode) -> TreeNode: - def dfs(root): - if root is None: - return - dfs(root.left) - nonlocal ans, prev - if prev == p: + def inorderSuccessor(self, root: TreeNode, p: TreeNode) -> Optional[TreeNode]: + ans = None + while root: + if root.val > p.val: ans = root - prev = root - dfs(root.right) - - ans = prev = None - dfs(root) + root = root.left + else: + root = root.right return ans ``` @@ -95,28 +101,17 @@ class Solution: * } */ class Solution { - private TreeNode prev; - private TreeNode p; - private TreeNode ans; - public TreeNode inorderSuccessor(TreeNode root, TreeNode p) { - prev = null; - ans = null; - this.p = p; - dfs(root); - return ans; - } - - private void dfs(TreeNode root) { - if (root == null) { - return; - } - dfs(root.left); - if (prev == p) { - ans = root; + TreeNode ans = null; + while (root != null) { + if (root.val > p.val) { + ans = root; + root = root.left; + } else { + root = root.right; + } } - prev = root; - dfs(root.right); + return ans; } } ``` @@ -133,23 +128,18 @@ class Solution { */ class Solution { public: - TreeNode* prev; - TreeNode* p; - TreeNode* ans; - TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) { - this->p = p; - dfs(root); + TreeNode* ans = nullptr; + while (root) { + if (root->val > p->val) { + ans = root; + root = root->left; + } else { + root = root->right; + } + } return ans; } - - void dfs(TreeNode* root) { - if (!root) return; - dfs(root->left); - if (prev == p) ans = root; - prev = root; - dfs(root->right); - } }; ``` @@ -162,91 +152,46 @@ public: * Right *TreeNode * } */ -func inorderSuccessor(root *TreeNode, p *TreeNode) *TreeNode { - var prev, ans *TreeNode - var dfs func(root *TreeNode) - dfs = func(root *TreeNode) { - if root == nil { - return - } - dfs(root.Left) - if prev == p { +func inorderSuccessor(root *TreeNode, p *TreeNode) (ans *TreeNode) { + for root != nil { + if root.Val > p.Val { ans = root + root = root.Left + } else { + root = root.Right } - prev = root - dfs(root.Right) } - dfs(root) - return ans + return } ``` -```js +```ts /** * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } * } */ -/** - * @param {TreeNode} root - * @param {TreeNode} p - * @return {TreeNode} - */ -var inorderSuccessor = function (root, p) { - if (root == null) { - return root; - } - const { val, left, right } = root; - const res = inorderSuccessor(left, p); - if (res != null) { - return res; - } - if (val > p.val) { - return root; - } - return inorderSuccessor(right, p); -}; -``` - - - -### Solution 2 - - -```cpp -/** - * Definition for a binary tree node. - * struct TreeNode { - * int val; - * TreeNode *left; - * TreeNode *right; - * TreeNode(int x) : val(x), left(NULL), right(NULL) {} - * }; - */ -class Solution { -public: - TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) { - stack stk; - TreeNode* cur = root; - while (cur != nullptr || !stk.empty()) { - if (cur == nullptr) { - cur = stk.top(); - stk.pop(); - if (cur->val > p->val) { - return cur; - } - cur = cur->right; - } else { - stk.push(cur); - cur = cur->left; - } +function inorderSuccessor(root: TreeNode | null, p: TreeNode | null): TreeNode | null { + let ans: TreeNode | null = null; + while (root) { + if (root.val > p.val) { + ans = root; + root = root.left; + } else { + root = root.right; } - return cur; } -}; + return ans; +} ``` ```js @@ -263,21 +208,16 @@ public: * @return {TreeNode} */ var inorderSuccessor = function (root, p) { - const stack = []; - let cur = root; - while (cur != null || stack.length !== 0) { - if (cur == null) { - cur = stack.pop(); - if (cur.val > p.val) { - return cur; - } - cur = cur.right; + let ans = null; + while (root) { + if (root.val > p.val) { + ans = root; + root = root.left; } else { - stack.push(cur); - cur = cur.left; + root = root.right; } } - return cur; + return ans; }; ``` diff --git a/lcci/04.06.Successor/Solution.cpp b/lcci/04.06.Successor/Solution.cpp index e8eccf423ff8c..5f6d680b97a1a 100644 --- a/lcci/04.06.Successor/Solution.cpp +++ b/lcci/04.06.Successor/Solution.cpp @@ -9,21 +9,16 @@ */ class Solution { public: - TreeNode* prev; - TreeNode* p; - TreeNode* ans; - TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) { - this->p = p; - dfs(root); + TreeNode* ans = nullptr; + while (root) { + if (root->val > p->val) { + ans = root; + root = root->left; + } else { + root = root->right; + } + } return ans; } - - void dfs(TreeNode* root) { - if (!root) return; - dfs(root->left); - if (prev == p) ans = root; - prev = root; - dfs(root->right); - } }; \ No newline at end of file diff --git a/lcci/04.06.Successor/Solution.go b/lcci/04.06.Successor/Solution.go index 71603977bfb18..ba29ea5066acb 100644 --- a/lcci/04.06.Successor/Solution.go +++ b/lcci/04.06.Successor/Solution.go @@ -6,20 +6,14 @@ * Right *TreeNode * } */ -func inorderSuccessor(root *TreeNode, p *TreeNode) *TreeNode { - var prev, ans *TreeNode - var dfs func(root *TreeNode) - dfs = func(root *TreeNode) { - if root == nil { - return - } - dfs(root.Left) - if prev == p { +func inorderSuccessor(root *TreeNode, p *TreeNode) (ans *TreeNode) { + for root != nil { + if root.Val > p.Val { ans = root + root = root.Left + } else { + root = root.Right } - prev = root - dfs(root.Right) } - dfs(root) - return ans + return } \ No newline at end of file diff --git a/lcci/04.06.Successor/Solution.java b/lcci/04.06.Successor/Solution.java index db7f267f88317..e388d684d44fc 100644 --- a/lcci/04.06.Successor/Solution.java +++ b/lcci/04.06.Successor/Solution.java @@ -8,27 +8,16 @@ * } */ class Solution { - private TreeNode prev; - private TreeNode p; - private TreeNode ans; - public TreeNode inorderSuccessor(TreeNode root, TreeNode p) { - prev = null; - ans = null; - this.p = p; - dfs(root); - return ans; - } - - private void dfs(TreeNode root) { - if (root == null) { - return; + TreeNode ans = null; + while (root != null) { + if (root.val > p.val) { + ans = root; + root = root.left; + } else { + root = root.right; + } } - dfs(root.left); - if (prev == p) { - ans = root; - } - prev = root; - dfs(root.right); + return ans; } } \ No newline at end of file diff --git a/lcci/04.06.Successor/Solution.js b/lcci/04.06.Successor/Solution.js index a8b6074c7fd85..ac3f5da50910a 100644 --- a/lcci/04.06.Successor/Solution.js +++ b/lcci/04.06.Successor/Solution.js @@ -11,16 +11,14 @@ * @return {TreeNode} */ var inorderSuccessor = function (root, p) { - if (root == null) { - return root; + let ans = null; + while (root) { + if (root.val > p.val) { + ans = root; + root = root.left; + } else { + root = root.right; + } } - const { val, left, right } = root; - const res = inorderSuccessor(left, p); - if (res != null) { - return res; - } - if (val > p.val) { - return root; - } - return inorderSuccessor(right, p); + return ans; }; diff --git a/lcci/04.06.Successor/Solution.py b/lcci/04.06.Successor/Solution.py index b08ad4991a5e1..06c22dfa72df1 100644 --- a/lcci/04.06.Successor/Solution.py +++ b/lcci/04.06.Successor/Solution.py @@ -7,17 +7,12 @@ class Solution: - def inorderSuccessor(self, root: TreeNode, p: TreeNode) -> TreeNode: - def dfs(root): - if root is None: - return - dfs(root.left) - nonlocal ans, prev - if prev == p: + def inorderSuccessor(self, root: TreeNode, p: TreeNode) -> Optional[TreeNode]: + ans = None + while root: + if root.val > p.val: ans = root - prev = root - dfs(root.right) - - ans = prev = None - dfs(root) + root = root.left + else: + root = root.right return ans diff --git a/lcci/04.06.Successor/Solution.ts b/lcci/04.06.Successor/Solution.ts new file mode 100644 index 0000000000000..f0861890e6b6a --- /dev/null +++ b/lcci/04.06.Successor/Solution.ts @@ -0,0 +1,26 @@ +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ + +function inorderSuccessor(root: TreeNode | null, p: TreeNode | null): TreeNode | null { + let ans: TreeNode | null = null; + while (root) { + if (root.val > p.val) { + ans = root; + root = root.left; + } else { + root = root.right; + } + } + return ans; +} diff --git a/lcci/04.06.Successor/Solution2.cpp b/lcci/04.06.Successor/Solution2.cpp deleted file mode 100644 index 3bca6ead22e7d..0000000000000 --- a/lcci/04.06.Successor/Solution2.cpp +++ /dev/null @@ -1,30 +0,0 @@ -/** - * Definition for a binary tree node. - * struct TreeNode { - * int val; - * TreeNode *left; - * TreeNode *right; - * TreeNode(int x) : val(x), left(NULL), right(NULL) {} - * }; - */ -class Solution { -public: - TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) { - stack stk; - TreeNode* cur = root; - while (cur != nullptr || !stk.empty()) { - if (cur == nullptr) { - cur = stk.top(); - stk.pop(); - if (cur->val > p->val) { - return cur; - } - cur = cur->right; - } else { - stk.push(cur); - cur = cur->left; - } - } - return cur; - } -}; \ No newline at end of file diff --git a/lcci/04.06.Successor/Solution2.js b/lcci/04.06.Successor/Solution2.js deleted file mode 100644 index 3dd723e651175..0000000000000 --- a/lcci/04.06.Successor/Solution2.js +++ /dev/null @@ -1,29 +0,0 @@ -/** - * Definition for a binary tree node. - * function TreeNode(val) { - * this.val = val; - * this.left = this.right = null; - * } - */ -/** - * @param {TreeNode} root - * @param {TreeNode} p - * @return {TreeNode} - */ -var inorderSuccessor = function (root, p) { - const stack = []; - let cur = root; - while (cur != null || stack.length !== 0) { - if (cur == null) { - cur = stack.pop(); - if (cur.val > p.val) { - return cur; - } - cur = cur.right; - } else { - stack.push(cur); - cur = cur.left; - } - } - return cur; -}; diff --git a/solution/0200-0299/0285.Inorder Successor in BST/README_EN.md b/solution/0200-0299/0285.Inorder Successor in BST/README_EN.md index c6c1fbb489ba1..62908329459e1 100644 --- a/solution/0200-0299/0285.Inorder Successor in BST/README_EN.md +++ b/solution/0200-0299/0285.Inorder Successor in BST/README_EN.md @@ -38,7 +38,18 @@ ## Solutions -### Solution 1 +### Solution 1: Binary Search + +The in-order traversal of a binary search tree is an ascending sequence, so we can use the binary search method. + +The in-order successor node of a binary search tree node $p$ satisfies: + +1. The value of the in-order successor node is greater than the value of node $p$. +2. The in-order successor is the node with the smallest value among all nodes greater than $p$. + +Therefore, for the current node $root$, if $root.val > p.val$, then $root$ could be the in-order successor of $p$. We record $root$ as $ans$ and then search the left subtree, i.e., $root = root.left$. If $root.val \leq p.val$, then $root$ cannot be the in-order successor of $p$, and we search the right subtree, i.e., $root = root.right$. + +The time complexity is $O(h)$, where $h$ is the height of the binary search tree. The space complexity is $O(1)$. diff --git a/solution/0500-0599/0510.Inorder Successor in BST II/README.md b/solution/0500-0599/0510.Inorder Successor in BST II/README.md index 50e6329f28507..b564bc890f7d2 100644 --- a/solution/0500-0599/0510.Inorder Successor in BST II/README.md +++ b/solution/0500-0599/0510.Inorder Successor in BST II/README.md @@ -85,7 +85,13 @@ class Node { ## 解法 -### 方法一 +### 方法一:分情况讨论 + +如果 $\text{node}$ 有右子树,那么 $\text{node}$ 的中序后继节点是右子树中最左边的节点。 + +如果 $\text{node}$ 没有右子树,那么如果 $\text{node}$ 是其父节点的右子树,我们就一直向上搜索,直到节点的父节点为空,或者节点是其父节点的左子树,此时父节点就是中序后继节点。 + +时间复杂度 $O(h)$,其中 $h$ 是二叉树的高度。空间复杂度 $O(1)$。 @@ -100,7 +106,6 @@ class Node: self.parent = None """ - class Solution: def inorderSuccessor(self, node: 'Node') -> 'Optional[Node]': if node.right: @@ -108,7 +113,7 @@ class Solution: while node.left: node = node.left return node - while node.parent and node == node.parent.right: + while node.parent and node.parent.right is node: node = node.parent return node.parent ``` @@ -125,7 +130,6 @@ class Node { */ class Solution { - public Node inorderSuccessor(Node node) { if (node.right != null) { node = node.right; @@ -134,7 +138,7 @@ class Solution { } return node; } - while (node.parent != null && node == node.parent.right) { + while (node.parent != null && node.parent.right == node) { node = node.parent; } return node.parent; @@ -159,10 +163,14 @@ public: Node* inorderSuccessor(Node* node) { if (node->right) { node = node->right; - while (node->left) node = node->left; + while (node->left) { + node = node->left; + } return node; } - while (node->parent && node == node->parent->right) node = node->parent; + while (node->parent && node->parent->right == node) { + node = node->parent; + } return node->parent; } }; @@ -194,6 +202,38 @@ func inorderSuccessor(node *Node) *Node { } ``` +```ts +/** + * Definition for a binary tree node. + * class Node { + * val: number + * left: Node | null + * right: Node | null + * parent: Node | null + * constructor(val?: number, left?: Node | null, right?: Node | null, parent?: Node | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * this.parent = (parent===undefined ? null : parent) + * } + * } + */ + +function inorderSuccessor(node: Node | null): Node | null { + if (node.right) { + node = node.right; + while (node.left) { + node = node.left; + } + return node; + } + while (node.parent && node === node.parent.right) { + node = node.parent; + } + return node.parent; +} +``` + ```js /** * // Definition for a Node. @@ -212,10 +252,14 @@ func inorderSuccessor(node *Node) *Node { var inorderSuccessor = function (node) { if (node.right) { node = node.right; - while (node.left) node = node.left; + while (node.left) { + node = node.left; + } return node; } - while (node.parent && node == node.parent.right) node = node.parent; + while (node.parent && node === node.parent.right) { + node = node.parent; + } return node.parent; }; ``` diff --git a/solution/0500-0599/0510.Inorder Successor in BST II/README_EN.md b/solution/0500-0599/0510.Inorder Successor in BST II/README_EN.md index a478900c6d1f8..585771df03fa6 100644 --- a/solution/0500-0599/0510.Inorder Successor in BST II/README_EN.md +++ b/solution/0500-0599/0510.Inorder Successor in BST II/README_EN.md @@ -52,7 +52,13 @@ class Node { ## Solutions -### Solution 1 +### Solution 1: Case Discussion + +If the `node` has a right subtree, then the in-order successor of `node` is the leftmost node in the right subtree. + +If the `node` does not have a right subtree, then if `node` is the right child of its parent, we continue to search upwards until the parent of the node is null, or the node is the left child of its parent. In this case, the parent node is the in-order successor. + +The time complexity is $O(h)$, where $h$ is the height of the binary tree. The space complexity is $O(1)$. @@ -67,7 +73,6 @@ class Node: self.parent = None """ - class Solution: def inorderSuccessor(self, node: 'Node') -> 'Optional[Node]': if node.right: @@ -75,7 +80,7 @@ class Solution: while node.left: node = node.left return node - while node.parent and node == node.parent.right: + while node.parent and node.parent.right is node: node = node.parent return node.parent ``` @@ -92,7 +97,6 @@ class Node { */ class Solution { - public Node inorderSuccessor(Node node) { if (node.right != null) { node = node.right; @@ -101,7 +105,7 @@ class Solution { } return node; } - while (node.parent != null && node == node.parent.right) { + while (node.parent != null && node.parent.right == node) { node = node.parent; } return node.parent; @@ -126,10 +130,14 @@ public: Node* inorderSuccessor(Node* node) { if (node->right) { node = node->right; - while (node->left) node = node->left; + while (node->left) { + node = node->left; + } return node; } - while (node->parent && node == node->parent->right) node = node->parent; + while (node->parent && node->parent->right == node) { + node = node->parent; + } return node->parent; } }; @@ -161,6 +169,38 @@ func inorderSuccessor(node *Node) *Node { } ``` +```ts +/** + * Definition for a binary tree node. + * class Node { + * val: number + * left: Node | null + * right: Node | null + * parent: Node | null + * constructor(val?: number, left?: Node | null, right?: Node | null, parent?: Node | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * this.parent = (parent===undefined ? null : parent) + * } + * } + */ + +function inorderSuccessor(node: Node | null): Node | null { + if (node.right) { + node = node.right; + while (node.left) { + node = node.left; + } + return node; + } + while (node.parent && node === node.parent.right) { + node = node.parent; + } + return node.parent; +} +``` + ```js /** * // Definition for a Node. @@ -179,10 +219,14 @@ func inorderSuccessor(node *Node) *Node { var inorderSuccessor = function (node) { if (node.right) { node = node.right; - while (node.left) node = node.left; + while (node.left) { + node = node.left; + } return node; } - while (node.parent && node == node.parent.right) node = node.parent; + while (node.parent && node === node.parent.right) { + node = node.parent; + } return node.parent; }; ``` diff --git a/solution/0500-0599/0510.Inorder Successor in BST II/Solution.cpp b/solution/0500-0599/0510.Inorder Successor in BST II/Solution.cpp index 9c0a7f8119529..8f6b4da2e5a42 100644 --- a/solution/0500-0599/0510.Inorder Successor in BST II/Solution.cpp +++ b/solution/0500-0599/0510.Inorder Successor in BST II/Solution.cpp @@ -14,10 +14,14 @@ class Solution { Node* inorderSuccessor(Node* node) { if (node->right) { node = node->right; - while (node->left) node = node->left; + while (node->left) { + node = node->left; + } return node; } - while (node->parent && node == node->parent->right) node = node->parent; + while (node->parent && node->parent->right == node) { + node = node->parent; + } return node->parent; } }; \ No newline at end of file diff --git a/solution/0500-0599/0510.Inorder Successor in BST II/Solution.java b/solution/0500-0599/0510.Inorder Successor in BST II/Solution.java index 4c08acff226cc..9e5de2ff2a277 100644 --- a/solution/0500-0599/0510.Inorder Successor in BST II/Solution.java +++ b/solution/0500-0599/0510.Inorder Successor in BST II/Solution.java @@ -9,7 +9,6 @@ class Node { */ class Solution { - public Node inorderSuccessor(Node node) { if (node.right != null) { node = node.right; @@ -18,7 +17,7 @@ public Node inorderSuccessor(Node node) { } return node; } - while (node.parent != null && node == node.parent.right) { + while (node.parent != null && node.parent.right == node) { node = node.parent; } return node.parent; diff --git a/solution/0500-0599/0510.Inorder Successor in BST II/Solution.js b/solution/0500-0599/0510.Inorder Successor in BST II/Solution.js index 0285729f7be08..534cdf5979773 100644 --- a/solution/0500-0599/0510.Inorder Successor in BST II/Solution.js +++ b/solution/0500-0599/0510.Inorder Successor in BST II/Solution.js @@ -15,9 +15,13 @@ var inorderSuccessor = function (node) { if (node.right) { node = node.right; - while (node.left) node = node.left; + while (node.left) { + node = node.left; + } return node; } - while (node.parent && node == node.parent.right) node = node.parent; + while (node.parent && node === node.parent.right) { + node = node.parent; + } return node.parent; }; diff --git a/solution/0500-0599/0510.Inorder Successor in BST II/Solution.py b/solution/0500-0599/0510.Inorder Successor in BST II/Solution.py index 7d760301ad933..19d84891dfd60 100644 --- a/solution/0500-0599/0510.Inorder Successor in BST II/Solution.py +++ b/solution/0500-0599/0510.Inorder Successor in BST II/Solution.py @@ -10,12 +10,12 @@ def __init__(self, val): class Solution: - def inorderSuccessor(self, node: 'Node') -> 'Optional[Node]': + def inorderSuccessor(self, node: "Node") -> "Optional[Node]": if node.right: node = node.right while node.left: node = node.left return node - while node.parent and node == node.parent.right: + while node.parent and node.parent.right is node: node = node.parent return node.parent diff --git a/solution/0500-0599/0510.Inorder Successor in BST II/Solution.ts b/solution/0500-0599/0510.Inorder Successor in BST II/Solution.ts new file mode 100644 index 0000000000000..c0931d66df92a --- /dev/null +++ b/solution/0500-0599/0510.Inorder Successor in BST II/Solution.ts @@ -0,0 +1,29 @@ +/** + * Definition for a binary tree node. + * class Node { + * val: number + * left: Node | null + * right: Node | null + * parent: Node | null + * constructor(val?: number, left?: Node | null, right?: Node | null, parent?: Node | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * this.parent = (parent===undefined ? null : parent) + * } + * } + */ + +function inorderSuccessor(node: Node | null): Node | null { + if (node.right) { + node = node.right; + while (node.left) { + node = node.left; + } + return node; + } + while (node.parent && node === node.parent.right) { + node = node.parent; + } + return node.parent; +}