TreeNode* min_node = FindMin(root); cout << "The minimum value in the tree is: " << min_node->val << endl; return 0; }用一段代码测试以上代码
时间: 2024-05-06 19:21:51 浏览: 67
确保测试代码中包含有Tree的数据结构定义和FindMin函数的实现。
下面是一个简单的示例:
```c++
#include <iostream>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
TreeNode* FindMin(TreeNode* root){
if(root->left != NULL)
return FindMin(root->left);
else
return root;
}
int main() {
// Create a test tree
TreeNode* root = new TreeNode(5);
root->left = new TreeNode(4);
root->right = new TreeNode(6);
root->left->left = new TreeNode(2);
root->left->right = new TreeNode(3);
root->left->left->left = new TreeNode(1);
// Find and print the minimum value in the tree
TreeNode* min_node = FindMin(root);
cout << "The minimum value in the tree is: " << min_node->val << endl;
return 0;
}
```
测试结果会输出:The minimum value in the tree is: 1
阅读全文