将BST的子节点替换为Rust中的孙子节点
我正在尝试删除节点。当节点没有子节点时,我只需修改父节点的左侧或右侧。当节点具有左子树或右子树时,如何删除节点并保留其子树 下面是TreeNode的实现:将BST的子节点替换为Rust中的孙子节点,rust,binary-search-tree,Rust,Binary Search Tree,我正在尝试删除节点。当节点没有子节点时,我只需修改父节点的左侧或右侧。当节点具有左子树或右子树时,如何删除节点并保留其子树 下面是TreeNode的实现: use std::rc::Rc; use std::cell::RefCell; // Definition for a binary tree node. #[derive(Debug, PartialEq, Eq)] pub struct TreeNode { pub val: i32, pub left: Option<
use std::rc::Rc;
use std::cell::RefCell;
// Definition for a binary tree node.
#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
pub val: i32,
pub left: Option<Rc<RefCell<TreeNode>>>,
pub right: Option<Rc<RefCell<TreeNode>>>,
}
impl TreeNode {
#[inline]
pub fn new(val: i32) -> Self {
TreeNode {
val,
left: None,
right: None
}
}
}
使用std::rc::rc;
使用std::cell::RefCell;
//二叉树节点的定义。
#[导出(调试、部分q、Eq)]
树状结构酒店{
发布日期:i32,
左:选项,
酒吧权利:选择权,
}
内隐树烯{
#[内联]
新发布(val:i32)->Self{
三烯醇{
瓦尔,
左:没有,
右:没有
}
}
}
实现是给定的,不能更改。我自己解决了:
let right_node = node.as_mut().unwrap().borrow_mut().right.take().unwrap(); // It may be the left child.
node.replace(right_node);