给出二叉数根节点,如何把这颗数在控制台以树形输出
C/C++ codeclass node{
public :
    node* children[2];
    string value;
}方法
void showTree(node* root){};
谁做过类似的的
()

解决方案 »

  1.   

    递归深度遍历.要树形输出得加个参数.开始时调用showTree(root,1);
    void showTree(node* root,int n)
    {
       if(!root) return ;
       if((null == root.chileren[0])&&(null == root.chileren[1])) return;
       else 
       {
                   for(int i = 0;i < n; i++) printf("-");
                   printf("&d\n",root.value);
                   if(!root.chileren[0]) showTree(root.chileren[0],n++);
                   if(!root.chileren[1]) showTree(root.chileren[1],n++);
       }
    }
      

  2.   

    代码得修改下.void showTree(node* root,int n)
    {
       if(!root) return ;
       if((null == root.chileren[0])&&(null == root.chileren[1])) return;
       else 
       {
                   for(int i = 0;i < n; i++) printf("-");
                   printf("&d\n",root.value);
                   if(!root.chileren[0]) showTree(root.chileren[0],n + 1);
                   if(!root.chileren[1]) showTree(root.chileren[1],n + 1);
       }
    }