1
我能打印我的表達式樹的序。但我需要能夠用圓括號打印它。例如後序53+應該輸出(5 + 3)打印表達式樹
我目前有:
void printTree(struct TreeNode *tree)
{
if (tree != NULL)
{
if (isalpha(tree->info) || isdigit(tree->info))
cout << "(";
printTree(tree->left);
cout<< tree->info;
printTree(tree->right);
if (isalpha(tree->info) || isdigit(tree->info))
cout << ")";
}
}
但是這給了我不正確的輸出。如果我輸入後綴表達式5分之62+它給我(6)/(2)+(5)
:(
什麼是 「序」? –
@Tomalak:http://en.wikipedia.org/wiki/Tree_traversal#Depth-first_Traversal –
@SanderDeDycker:鉭 –