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 浏览: 71
确保测试代码中包含有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
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)