mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
Merge branch 'master' into binary_search_tree
This commit is contained in:
@ -162,7 +162,7 @@ namespace hello_algo.chapter_tree
|
||||
else
|
||||
{
|
||||
// 子结点数量 = 2 ,则将中序遍历的下个结点删除,并用该结点替换当前结点
|
||||
TreeNode? temp = minNode(node.right);
|
||||
TreeNode? temp = getInOrderNext(node.right);
|
||||
node.right = removeHelper(node.right, temp.val);
|
||||
node.val = temp.val;
|
||||
}
|
||||
@ -174,8 +174,8 @@ namespace hello_algo.chapter_tree
|
||||
return node;
|
||||
}
|
||||
|
||||
/* 获取最小结点 */
|
||||
private TreeNode? minNode(TreeNode? node)
|
||||
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
|
||||
private TreeNode? getInOrderNext(TreeNode? node)
|
||||
{
|
||||
if (node == null) return node;
|
||||
// 循环访问左子结点,直到叶结点时为最小结点,跳出
|
||||
|
||||
@ -35,11 +35,7 @@ namespace hello_algo.chapter_tree
|
||||
return root;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查找结点
|
||||
/// </summary>
|
||||
/// <param name="num"></param>
|
||||
/// <returns></returns>
|
||||
/* 查找结点 */
|
||||
public TreeNode? search(int num)
|
||||
{
|
||||
TreeNode? cur = root;
|
||||
@ -125,7 +121,7 @@ namespace hello_algo.chapter_tree
|
||||
else
|
||||
{
|
||||
// 获取中序遍历中 cur 的下一个结点
|
||||
TreeNode? nex = min(cur.right);
|
||||
TreeNode? nex = getInOrderNext(cur.right);
|
||||
if (nex != null)
|
||||
{
|
||||
int tmp = nex.val;
|
||||
@ -138,8 +134,8 @@ namespace hello_algo.chapter_tree
|
||||
return cur;
|
||||
}
|
||||
|
||||
/* 获取最小结点 */
|
||||
private TreeNode? min(TreeNode? root)
|
||||
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
|
||||
private TreeNode? getInOrderNext(TreeNode? root)
|
||||
{
|
||||
if (root == null) return root;
|
||||
// 循环访问左子结点,直到叶结点时为最小结点,跳出
|
||||
|
||||
@ -12,11 +12,7 @@ namespace hello_algo.chapter_tree
|
||||
public class binary_tree_bfs
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// 层序遍历
|
||||
/// </summary>
|
||||
/// <param name="root"></param>
|
||||
/// <returns></returns>
|
||||
/* 层序遍历 */
|
||||
public List<int> hierOrder(TreeNode root)
|
||||
{
|
||||
// 初始化队列,加入根结点
|
||||
@ -41,8 +37,7 @@ namespace hello_algo.chapter_tree
|
||||
{
|
||||
/* 初始化二叉树 */
|
||||
// 这里借助了一个从数组直接生成二叉树的函数
|
||||
TreeNode? root = TreeNode.ArrToTree(new int?[] {
|
||||
1, 2, 3, 4, 5, 6, 7, null, null, null, null, null, null, null, null});
|
||||
TreeNode? root = TreeNode.ArrToTree(new int?[] { 1, 2, 3, 4, 5, 6, 7 });
|
||||
Console.WriteLine("\n初始化二叉树\n");
|
||||
PrintUtil.PrintTree(root);
|
||||
|
||||
|
||||
@ -13,10 +13,7 @@ namespace hello_algo.chapter_tree
|
||||
{
|
||||
List<int> list = new();
|
||||
|
||||
/// <summary>
|
||||
/// 前序遍历
|
||||
/// </summary>
|
||||
/// <param name="root"></param>
|
||||
/* 前序遍历 */
|
||||
void preOrder(TreeNode? root)
|
||||
{
|
||||
if (root == null) return;
|
||||
@ -26,10 +23,7 @@ namespace hello_algo.chapter_tree
|
||||
preOrder(root.right);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 中序遍历
|
||||
/// </summary>
|
||||
/// <param name="root"></param>
|
||||
/* 中序遍历 */
|
||||
void inOrder(TreeNode? root)
|
||||
{
|
||||
if (root == null) return;
|
||||
@ -39,10 +33,7 @@ namespace hello_algo.chapter_tree
|
||||
inOrder(root.right);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 后序遍历
|
||||
/// </summary>
|
||||
/// <param name="root"></param>
|
||||
/* 后序遍历 */
|
||||
void postOrder(TreeNode? root)
|
||||
{
|
||||
if (root == null) return;
|
||||
@ -57,8 +48,7 @@ namespace hello_algo.chapter_tree
|
||||
{
|
||||
/* 初始化二叉树 */
|
||||
// 这里借助了一个从数组直接生成二叉树的函数
|
||||
TreeNode? root = TreeNode.ArrToTree(new int?[] {
|
||||
1, 2, 3, 4, 5, 6, 7, null, null, null, null, null, null, null, null});
|
||||
TreeNode? root = TreeNode.ArrToTree(new int?[] { 1, 2, 3, 4, 5, 6, 7 });
|
||||
Console.WriteLine("\n初始化二叉树\n");
|
||||
PrintUtil.PrintTree(root);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user