已知二叉树先序和中序序列 求后序序列

来源:互联网 发布:彩票开奖平台源码 编辑:程序博客网 时间:2024/06/01 22:48

源码如下:

void test(std::string str1,std::string str2,int length){if(str1.empty()&&str2.empty())return;s.push(str1.c_str()[0]);int count=str2.find_first_of(s.top());std::string str11,str22;count++;str11=str1.substr(count,length-count);str22=str2.substr(count,length-count);test(str11,str22,str11.length());str11=str1.substr(1,--count);str22=str2.substr(0,count);test(str11,str22,str22.length());}int main(){test("abcdefopghi","dcebofpahgi",11);while(!s.empty()){cout<<s.top()<<" ";s.pop();}}


递推公式

test(*str)

{

     if(!str) return;

      push_back(str[0]);

      substr=右子树;

      test(substr);

      substr=左子树;

      test(substr);

}