510. Inorder successor bst II
510_solution1.c
#include "node.h"
static struct Node *findMinKeyWithinTree(struct Node *root) {
while (root->left)
root = root->left;
return root;
}
struct Node *inorderSuccessor(struct Node *node) {
if (node->right)
return findMinKeyWithinTree(node->right);
struct Node *ancestor = node->parent;
struct Node *child = node;
while (ancestor && child == ancestor->right) {
child = ancestor;
ancestor = child->parent;
}
return ancestor;
}
node.h
#pragma once
#include <stddef.h>
// Definition for a binary tree node with a parent pointer.
struct Node {
int val;
struct Node *left;
struct Node *right;
struct Node *parent;
};
510_solution1.cpp
#include "node.h"
class Solution {
Node *findMinKeyWithinTree(Node *root) {
while (root->left)
root = root->left;
return root;
}
public:
Node *inorderSuccessor(Node *node) {
if (node->right)
return findMinKeyWithinTree(node->right);
Node *ancestor = node->parent;
Node *child = node;
while (ancestor && child == ancestor->right) {
child = ancestor;
ancestor = child->parent;
}
return ancestor;
}
};
node.h
#pragma once
// Definition for a binary tree node with a parent pointer.
struct Node {
int val;
Node *left = nullptr;
Node *right = nullptr;
Node *parent = nullptr;
Node(int x) : val(x) {}
};
510_solution1.go
package trees
func findMinKeyWithinTree(root *Node) *Node {
for root.Left != nil {
root = root.Left
}
return root
}
func inorderSuccessor(node *Node) *Node {
if node.Right != nil {
return findMinKeyWithinTree(node.Right)
}
ancestor, child := node.Parent, node
for ancestor != nil && child == ancestor.Right {
child = ancestor
ancestor = child.Parent
}
return ancestor
}
node.go
package trees
// Definition for a binary tree node with a parent pointer.
type Node struct {
Val int
Left *Node
Right *Node
Parent *Node
}
510_solution1.java
class Solution {
private Node findMinKeyWithinTree(Node root) {
while (root.left != null)
root = root.left;
return root;
}
public Node inorderSuccessor(Node node) {
if (node.right != null)
return findMinKeyWithinTree(node.right);
Node ancestor = node.parent;
Node child = node;
while (ancestor != null && child == ancestor.right) {
child = ancestor;
ancestor = child.parent;
}
return ancestor;
}
}
Node.java
// Definition for a binary tree node with a parent pointer.
class Node {
public int val;
public Node left;
public Node right;
public Node parent;
}
510_solution1.js
const Node = require("./node");
const findMinKeyWithinTree = (root) => {
while (root.left) root = root.left;
return root;
};
var inorderSuccessor = function (node) {
if (node.right) return findMinKeyWithinTree(node.right);
let ancestor = node.parent;
let child = node;
while (ancestor && child === ancestor.right) {
child = ancestor;
ancestor = child.parent;
}
return ancestor;
};
node.js
// Definition for a binary tree node with a parent pointer.
function Node(val) {
this.val = val;
this.left = null;
this.right = null;
this.parent = null;
}
module.exports = Node;
510_solution1.py
from node import Node
class Solution:
def inorderSuccessor(self, node: Node) -> Node | None:
if node.right:
return self.findMinKeyWithinTree(node.right)
ancestor, child = node.parent, node
while ancestor and child is ancestor.right:
child = ancestor
ancestor = child.parent
return ancestor
def findMinKeyWithinTree(self, root: Node) -> Node:
while root.left:
root = root.left
return root
node.py
# Definition for a binary tree node with a parent pointer.
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
self.parent = None
510_solution1.rs
use std::cell::RefCell;
use std::rc::Rc;
use crate::node::Node;
type NodeRef = Rc<RefCell<Node>>;
impl Solution {
fn find_min_key_within_tree(mut root: NodeRef) -> NodeRef {
loop {
let left = root.borrow().left.clone();
match left {
Some(next) => root = next,
None => return root,
}
}
}
fn parent_of(node: &NodeRef) -> Option<NodeRef> {
node.borrow().parent.as_ref()?.upgrade()
}
pub fn inorder_successor(node: NodeRef) -> Option<NodeRef> {
if let Some(right) = node.borrow().right.clone() {
return Some(Self::find_min_key_within_tree(right));
}
let mut child = node.clone();
let mut ancestor = Self::parent_of(&node);
while let Some(a) = ancestor.clone() {
let is_right = a.borrow().right.as_ref().is_some_and(|r| Rc::ptr_eq(r, &child));
if !is_right {
break;
}
ancestor = Self::parent_of(&a);
child = a;
}
ancestor
}
}
node.rs
use std::cell::RefCell;
use std::rc::{Rc, Weak};
// Definition for a binary tree node with a parent pointer.
// The parent link is Weak so parent and child do not keep each other alive.
#[derive(Debug)]
pub struct Node {
pub val: i32,
pub left: Option<Rc<RefCell<Node>>>,
pub right: Option<Rc<RefCell<Node>>>,
pub parent: Option<Weak<RefCell<Node>>>,
}