题目网上没找到,先把自己写的代码贴上来。
#include <iostream> #include <deque> using namespace std; struct BinaryTreeNode { char ch; BinaryTreeNode *pLeft; BinaryTreeNode *pRight; }; int Depth(BinaryTreeNode *pRoot) { if (NULL == pRoot) { return 0; } int left = Depth(pRoot->pLeft); int right = Depth(pRoot->pRight); int max = (left > right ? left : right); return (max+1); } BinaryTreeNode* FindFirstNULL(BinaryTreeNode *pRoot, deque<BinaryTreeNode*> dq, int &flag) { if (NULL == pRoot) { return NULL; } dq.push_back(pRoot); BinaryTreeNode *pTemp; while (dq.size() > 0) { pTemp = dq.front(); dq.pop_front(); if (pTemp->pLeft!=NULL && pTemp->pRight!=NULL) { dq.push_back(pTemp->pLeft); dq.push_back(pTemp->pRight); } else if (NULL == pTemp->pLeft) { flag = 0; break; } else { flag = 1; break; } } return pTemp; } BinaryTreeNode* Merge(BinaryTreeNode *pTree1, BinaryTreeNode *pTree2) { int depth1 = Depth(pTree1); int depth2 = Depth(pTree2); int flag1, flag2; BinaryTreeNode *p1, *p2; deque<BinaryTreeNode *> dq1, dq2; p1 = FindFirstNULL(pTree1, dq1, flag1); p2 = FindFirstNULL(pTree2, dq2, flag2); int d1 = Depth(p1); int d2 = Depth(p2); if ((depth1-d1) > (depth2-d2)) { if (flag1 == 0) { p1->pLeft = pTree2; } else { p1->pRight = pTree2; } return pTree1; } else { if (flag2 == 0) { p2->pLeft = pTree1; } else { p2->pRight = pTree1; } return pTree2; } } void Insert(BinaryTreeNode *pRoot, char ch) { pRoot->ch = ch; pRoot->pLeft = pRoot->pRight = NULL; } void PrintInOrder(BinaryTreeNode *pRoot) { if (NULL == pRoot) { return ; } PrintInOrder(pRoot->pLeft); cout<<pRoot->ch<<endl; PrintInOrder(pRoot->pRight); } int main() { // 建立树1 BinaryTreeNode *pRoot1 = new BinaryTreeNode; Insert(pRoot1, 'A'); pRoot1->pLeft = new BinaryTreeNode; Insert(pRoot1->pLeft, 'B'); pRoot1->pRight = new BinaryTreeNode; Insert(pRoot1->pRight, 'C'); pRoot1->pRight->pRight = new BinaryTreeNode; Insert(pRoot1->pRight->pRight, 'D'); pRoot1->pRight->pRight->pRight = new BinaryTreeNode; Insert(pRoot1->pRight->pRight->pRight, 'E'); // 建立树2 BinaryTreeNode *pRoot2 = new BinaryTreeNode; Insert(pRoot2, 'a'); pRoot2->pLeft = new BinaryTreeNode; Insert(pRoot2->pLeft, 'b'); pRoot2->pRight= new BinaryTreeNode; Insert(pRoot2->pRight, 'c'); pRoot2->pLeft->pLeft = new BinaryTreeNode; Insert(pRoot2->pLeft->pLeft, 'd'); pRoot2->pRight->pRight = new BinaryTreeNode; Insert(pRoot2->pRight->pRight, 'e'); cout<<"树1的中序遍历"<<endl; PrintInOrder(pRoot1); cout<<endl; cout<<"树2的中序遍历"<<endl; PrintInOrder(pRoot2); cout<<endl; // 合并两棵树 BinaryTreeNode *root = Merge(pRoot1, pRoot2); cout<<"合并后的树的中序遍历"<<endl; PrintInOrder(root); system("pause"); return 0; }
#include <iostream> #include <vector> using namespace std; int FindNum(int m, int n) { int **mat = new int* [m]; for (int i=0; i<m; ++i) { mat[i] = new int [n]; } for (int i=0; i<m; ++i) { mat[i][0] = 1; } for (int j=0; j<n; ++j) { mat[0][j] = 1; } for (int i=1; i<m; ++i) { for (int j=1; j<n; ++j) { mat[i][j] = mat[i-1][j] + mat[i][j-1]; } } int num = mat[m-1][n-1]; return num; } static int num = 0; void FindPath(int i, int j, int m, int n, vector<int> v) { if (m==i && n==j) { cout<<++num<<":"; vector<int>::iterator i = v.begin(); for (; i<v.end(); ++i) { if (0 == *i) { if (i == (v.end()-1)) { cout<<"right"; } else { cout<<"right->"; } } else { if (i == (v.end() - 1)) { cout<<"down"; } else { cout<<"down->"; } } } cout<<endl; } if ((i+1) <=m) { v.push_back(1); FindPath(i+1, j, m, n, v); v.pop_back(); } if ((j+1) <= n) { v.push_back(0); FindPath(i, j+1, m, n, v); v.pop_back(); } } int main() { int m, n; cin>>m>>n; cout<<FindNum(m, n)<<endl; vector<int> v; FindPath(1, 1, m, n, v); system("pause"); return 0; }