二叉排序树递归插入结点,有些结点不见了...

二叉排序树递归插入结点,中序遍历结果是4,5,8。其他结点去哪儿了?麻烦给看看,谢谢!

#include<iostream> using namespace std; struct BinaryTree { int val; BinaryTree* left; BinaryTree* right; BinaryTree(int n) :left(NULL), right(NULL), val(n) {} }; BinaryTree* BST_insert(BinaryTree* root, int value) {  //我懵逼了!!! if (root == NULL) { BinaryTree* node = new BinaryTree(value); return node; } else { if (root->val > value) { root->left = BST_insert(root->left, value); } else { root->right = BST_insert(root->right, value); } } } void InorderTra(BinaryTree* root) { if (root == NULL) { return; } InorderTra(root->left); cout << root->val << " "; InorderTra(root->right); } int main() { BinaryTree* head = new BinaryTree(5); int arr[] = { 2,3,4,6,7,8}; for (int i = 0; i < 6; i++) { BST_insert(head, arr[i]); } InorderTra(head); cout << endl; return 0; }


#去哪儿##uc##笔试题目#
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务