首页 > 二叉排序树递归插入结点,有些结点不见了...
头像
夜风凛凛
编辑于 2019-11-11 03:50
+ 关注

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

二叉排序树递归插入结点,中序遍历结果是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; }


全部评论

(0) 回帖
加载中...
话题 回帖

近期热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

热门推荐