Inorder Successor in Binary Search Tree

Given a binary search tree (See Definition) and a node in it, find the in-order successor of that node in the BST.

If the given node has no in-order successor in the tree, return null.

使用中序遍历的方式进行求解

java

public class Solution {
    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
        if (root == null || p == null) {
            return null;
        }
        List<TreeNode> path = inOrder(root);
        int index = path.indexOf(p);
        return index == -1 || index == path.size() - 1 ? null : path.get(index + 1);
    }
    private List<TreeNode> inOrder(TreeNode root) {
        List<TreeNode> result = new ArrayList<>();
        if (root == null) {
            return result;
        }
        List<TreeNode> left = inOrder(root.left);
        List<TreeNode> right = inOrder(root.right);
        result.addAll(left);
        result.add(root);
        result.addAll(right);
        return result;
    }
}

使用二叉树的性质进行求解

public class Solution {
    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
        if (root == null || p == null) {
            return null;
        }
        if (root.val <= p.val) {
            return inorderSuccessor(root.right, p);
        } else {
            TreeNode left = inorderSuccessor(root.left, p);
            return left == null ? root : left;
        }
    }
}


以下是用C语言编写的程序实现上述功能: ```c #include <stdio.h> #include <stdlib.h> // 定义二叉树节点结构体 typedef struct node { char data; struct node *left; struct node *right; int ltag, rtag; // 线索标志 } Node; Node* create_binary_tree(char* s, int* i) { Node* root = NULL; if (s[*i] == '(') { // 非叶子节点 (*i)++; if (s[*i] != ')') { root = (Node*)malloc(sizeof(Node)); root->data = s[*i]; root->left = create_binary_tree(s, i); root->right = create_binary_tree(s, i); } (*i)++; } return root; } void print_binary_tree(Node* root, int depth) { if (root != NULL) { print_binary_tree(root->right, depth + 1); for (int i = 0; i < depth; i++) { printf(" "); } printf("%c\n", root->data); print_binary_tree(root->left, depth + 1); } } Node* search_node(Node* root, char data) { if (root == NULL) { return NULL; } else if (root->data == data) { return root; } else { Node* left_child = search_node(root->left, data); Node* right_child = search_node(root->right, data); if (left_child != NULL) { return left_child; } else { return right_child; } } } int get_height(Node* root) { if (root == NULL) { return 0; } else { int left_height = get_height(root->left); int right_height = get_height(root->right); return (left_height > right_height ? left_height : right_height) + 1; } } void free_binary_tree(Node* root) { if (root != NULL) { free_binary_tree(root->left); free_binary_tree(root->right); free(root); } } Node* build_binary_tree(char* preorder, char* inorder, int len) { if (len == 0) { return NULL; } else { Node* root = (Node*)malloc(sizeof(Node)); root->data = preorder[0]; int i; for (i = 0; i < len; i++) { if (inorder[i] == preorder[0]) { break; } } root->left = build_binary_tree(preorder + 1, inorder, i); root->right = build_binary_tree(preorder + i + 1, inorder + i + 1, len - i - 1); return root; } } Node* get_successor(Node* node) { if (node->rtag == 0) { return node->right; } else { Node* p = node->right; while (p->ltag == 1) { p = p->left; } return p; } } void inthread(Node* root, Node** pre) { if (root != NULL) { inthread(root->left, pre); if (root->left == NULL) { root->left = *pre; root->ltag = 1; } if (*pre != NULL && (*pre)->right == NULL) { (*pre)->right = root; (*pre)->rtag = 1; } *pre = root; inthread(root->right, pre); } } void inorder_traversal(Node* root) { Node* p = root; while (p->ltag == 1) { p = p->left; } while (p != NULL) { printf("%c ", p->data); p = get_successor(p); } } int main() { // 创建二叉树 char s[] = "A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))"; int i = 0; Node* root = create_binary_tree(s, &i); // 输出二叉树 printf("Binary tree:\n"); print_binary_tree(root, 0); // 输出'H'结点的左、右孩子结点值 Node* node_H = search_node(root, 'H'); printf("Node H's left child: %c\n", node_H->left->data); printf("Node H's right child: %c\n", node_H->right->data); // 输出二叉树高度 int height = get_height(root); printf("Height of binary tree: %d\n", height); // 释放二叉树 free_binary_tree(root); root = NULL; // 重新构造二叉树 char preorder[] = "ABDEHJKLNMCFGKI"; char inorder[] = "HJLKNMDEBKAFGIC"; int len = sizeof(preorder) / sizeof(char) - 1; root = build_binary_tree(preorder, inorder, len); // 输出二叉树 printf("Binary tree:\n"); print_binary_tree(root, 0); // 对二叉树进行中序线索化 Node* pre = NULL; inthread(root, &pre); // 输出中序线索二叉树的中序序列 printf("Inorder traversal of threaded binary tree:\n"); inorder_traversal(root); // 释放二叉树 free_binary_tree(root); root = NULL; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ncst

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值