运行 ❯
获取您
自己的
网站
×
更改方向
更改主题,深色/浅色
前往 Spaces
Python
C
Java
class TreeNode: def __init__(self, data): self.data = data self.left = None self.right = None def search(node, target): if node is None: return None elif node.data == target: return node elif target < node.data: return search(node.left, target) else: return search(node.right, target) root = TreeNode(13) node7 = TreeNode(7) node15 = TreeNode(15) node3 = TreeNode(3) node8 = TreeNode(8) node14 = TreeNode(14) node19 = TreeNode(19) node18 = TreeNode(18) root.left = node7 root.right = node15 node7.left = node3 node7.right = node8 node15.left = node14 node15.right = node19 node19.left = node18 # Search for a value result = search(root, 8) if result: print(f"Found the node with value: {result.data}") else: print("Value not found in the BST.") #Python
#include <stdio.h> #include <stdlib.h> typedef struct TreeNode { int data; struct TreeNode* left; struct TreeNode* right; } TreeNode; TreeNode* createNode(int data) { TreeNode* newNode = (TreeNode*)malloc(sizeof(TreeNode)); newNode->data = data; newNode->left = NULL; newNode->right = NULL; return newNode; } TreeNode* search(TreeNode* node, int target) { if (node == NULL) { return NULL; } else if (node->data == target) { return node; } else if (target < node->data) { return search(node->left, target); } else { return search(node->right, target); } } int main() { TreeNode* root = createNode(13); TreeNode* node7 = createNode(7); TreeNode* node15 = createNode(15); TreeNode* node3 = createNode(3); TreeNode* node8 = createNode(8); TreeNode* node14 = createNode(14); TreeNode* node19 = createNode(19); TreeNode* node18 = createNode(18); root->left = node7; root->right = node15; node7->left = node3; node7->right = node8; node15->left = node14; node15->right = node19; node19->left = node18; // Search for a value TreeNode* result = search(root, 8); if (result) { printf("Found the node with value: %d\n", result->data); } else { printf("Value not found in the BST.\n"); } // Free the memory (optional but good practice) free(node18); free(node19); free(node14); free(node8); free(node3); free(node15); free(node7); free(root); return 0; } //C
public class Main { static class TreeNode { int data; TreeNode left; TreeNode right; TreeNode(int data) { this.data = data; left = null; right = null; } } public static TreeNode search(TreeNode node, int target) { if (node == null) { return null; } else if (node.data == target) { return node; } else if (target < node.data) { return search(node.left, target); } else { return search(node.right, target); } } public static void main(String[] args) { TreeNode root = new TreeNode(13); TreeNode node7 = new TreeNode(7); TreeNode node15 = new TreeNode(15); TreeNode node3 = new TreeNode(3); TreeNode node8 = new TreeNode(8); TreeNode node14 = new TreeNode(14); TreeNode node19 = new TreeNode(19); TreeNode node18 = new TreeNode(18); root.left = node7; root.right = node15; node7.left = node3; node7.right = node8; node15.left = node14; node15.right = node19; node19.left = node18; // Search for a value TreeNode result = search(root, 8); if (result != null) { System.out.println("Found the node with value: " + result.data); } else { System.out.println("Value not found in the BST."); } } } // Java
Python 结果
C 结果
Java 结果
找到值为 8 的节点
找到值为 8 的节点
找到值为 8 的节点