运行 ❯
获取您
自己的
网站
×
更改方向
更改主题,深色/浅色
前往 Spaces
Python
C
Java
class TreeNode: def __init__(self, data): self.data = data self.left = None self.right = None def inOrderTraversal(node): if node is None: return inOrderTraversal(node.left) print(node.data, end=", ") inOrderTraversal(node.right) def minValueNode(node): current = node while current.left is not None: current = current.left return current 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 # Traverse inOrderTraversal(root) # Find Lowest print("\nLowest value:",minValueNode(root).data) #Python
#include <stdio.h> #include <stdlib.h> typedef struct TreeNode { int data; struct TreeNode* left; struct TreeNode* right; } TreeNode; TreeNode* newTreeNode(int data) { TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode)); node->data = data; node->left = NULL; node->right = NULL; return node; } void inOrderTraversal(TreeNode* node) { if (node == NULL) return; inOrderTraversal(node->left); printf("%d, ", node->data); inOrderTraversal(node->right); } TreeNode* minValueNode(TreeNode *node) { TreeNode *current = node; while (current && current->left != NULL) { current = current->left; } return current; } int main() { TreeNode* root = newTreeNode(13); root->left = newTreeNode(7); root->right = newTreeNode(15); root->left->left = newTreeNode(3); root->left->right = newTreeNode(8); root->right->left = newTreeNode(14); root->right->right = newTreeNode(19); root->right->right->left = newTreeNode(18); inOrderTraversal(root); printf("\n"); TreeNode *minNode = minValueNode(root); printf("Lowest value: %d\n", minNode->data); return 0; } //C
public class Main { public static class TreeNode { int data; TreeNode left; TreeNode right; TreeNode(int data) { this.data = data; left = null; right = null; } } public static void inOrderTraversal(TreeNode node) { if (node == null) return; inOrderTraversal(node.left); System.out.print(node.data + ", "); inOrderTraversal(node.right); } public static TreeNode minValueNode(TreeNode node) { TreeNode current = node; while (current != null && current.left != null) { current = current.left; } return current; } public static void main(String[] args) { TreeNode root = new TreeNode(13); root.left = new TreeNode(7); root.right = new TreeNode(15); root.left.left = new TreeNode(3); root.left.right = new TreeNode(8); root.right.left = new TreeNode(14); root.right.right = new TreeNode(19); root.right.right.left = new TreeNode(18); inOrderTraversal(root); System.out.println(); TreeNode minNode = minValueNode(root); System.out.println("Lowest value: " + minNode.data); } } // Java
Python 结果
C 结果
Java 结果
3, 7, 8, 13, 14, 15, 18, 19,
最小值:3
3, 7, 8, 13, 14, 15, 18, 19,
最小值:3
3, 7, 8, 13, 14, 15, 18, 19,
最小值:3