通过后遍历打印n元树

时间:2020-01-13 15:56:04

标签: recursion tree postorder

我陷入了一个问题,我应该通过事后遍历打印n元树(通用树)的内容。我正在使用递归遍历订单。仍然使用当前代码,输出仍不理想。

那么任何人都可以说我在做什么错/我应该做哪些更正?

 /**************
template <typename T>
class TreeNode {
public:
    T data;
    vector<TreeNode<T>*> children;

    TreeNode(T data) {
        this->data = data;
    }

    ~TreeNode() {
        for (int i = 0; i < children.size(); i++) {
            delete children[i];
        }
    }
   };
      ***************/

  void postOrder(TreeNode<int>* root) {
  if (root==NULL)
    return;

    if (root->children.size()==0)
    cout<<root->data<<" ";


for (int i=0;i<root->children.size();i++)
{
    postOrder(root->children[i]);
}
 }

被注释的部分是针对Tree类的,我只应该做一个打印函数。其余的会自动处理

0 个答案:

没有答案
相关问题