简单来说,就是二叉树的前序、中序、后序遍历,包括了递归和非递归的方法
前序遍历(注释中的为递归版本):
1 #include <vector> 2 #include <stack> 3 #include <stddef.h> 4 #include <iostream> 5 6 using namespace std; 7 8 struct TreeNode 9 { 10 int val; 11 TreeNode *left; 12 TreeNode *right; 13 TreeNode(int x) : val(x), left(NULL), right(NULL) {} 14 }; 15 16 class Solution 17 { 18 public: 19 vector<int> preorderTraversal(TreeNode *root) 20 { 21 /* 22 vector<int> rootVector; 23 vector<int> leftVector; 24 vector<int> rightVector; 25 26 if (root == NULL) 27 { 28 return rootVector; 29 } 30 31 rootVector.push_back(root->val); 32 leftVector = preorderTraversal(root->left); 33 rightVector = preorderTraversal(root->right); 34 rootVector.insert(rootVector.end(), leftVector.begin(), leftVector.end()); 35 rootVector.insert(rootVector.end(), rightVector.begin(), rightVector.end()); 36 37 return rootVector; 38 */ 39 40 vector<int> preorder; 41 stack<TreeNode *> treeNodeStack; 42 43 while (root || !treeNodeStack.empty()) 44 { 45 while (root) 46 { 47 preorder.push_back(root->val); 48 treeNodeStack.push(root); 49 root = root->left; 50 } 51 root = treeNodeStack.top(); 52 treeNodeStack.pop(); 53 root = root->right; 54 } 55 56 return preorder; 57 } 58 }; 59 60 int main() 61 { 62 Solution s; 63 TreeNode *root = new TreeNode(1); 64 root->right = new TreeNode(2); 65 root->right->left = new TreeNode(3); 66 67 vector<int> v = s.preorderTraversal(root); 68 69 for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) 70 { 71 cout << *it << endl; 72 } 73 74 system("pause"); 75 76 return 0; 77 }
中序遍历(注释中的为递归版本):
1 #include <vector> 2 #include <stack> 3 #include <stddef.h> 4 #include <iostream> 5 6 using namespace std; 7 8 struct TreeNode 9 { 10 int val; 11 TreeNode *left; 12 TreeNode *right; 13 TreeNode(int x) : val(x), left(NULL), right(NULL) {} 14 }; 15 16 class Solution 17 { 18 public: 19 vector<int> inorderTraversal(TreeNode *root) 20 { 21 /* 22 vector<int> rootVector; 23 vector<int> leftVector; 24 vector<int> rightVector; 25 26 if (root == NULL) 27 { 28 return rootVector; 29 } 30 31 leftVector = inorderTraversal(root->left); 32 rightVector = inorderTraversal(root->right); 33 rootVector.insert(rootVector.end(), leftVector.begin(), leftVector.end()); 34 rootVector.push_back(root->val); 35 rootVector.insert(rootVector.end(), rightVector.begin(), rightVector.end()); 36 37 return rootVector; 38 */ 39 40 vector<int> inorder; 41 stack<TreeNode *> treeNodeStack; 42 43 while (root || !treeNodeStack.empty()) 44 { 45 while (root) 46 { 47 treeNodeStack.push(root); 48 root = root->left; 49 } 50 51 root = treeNodeStack.top(); 52 treeNodeStack.pop(); 53 inorder.push_back(root->val); 54 root = root->right; 55 } 56 57 return inorder; 58 } 59 }; 60 61 int main() 62 { 63 Solution s; 64 TreeNode *root = new TreeNode(1); 65 root->right = new TreeNode(2); 66 root->right->left = new TreeNode(3); 67 68 vector<int> v = s.inorderTraversal(root); 69 70 for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) 71 { 72 cout << *it << endl; 73 } 74 75 system("pause"); 76 77 return 0; 78 }
后序遍历(注释中的为递归版本):
1 #include <vector> 2 #include <stack> 3 #include <stddef.h> 4 #include <iostream> 5 6 using namespace std; 7 8 struct TreeNode 9 { 10 int val; 11 TreeNode *left; 12 TreeNode *right; 13 TreeNode(int x) : val(x), left(NULL), right(NULL) {} 14 }; 15 16 class Solution 17 { 18 public: 19 vector<int> postorderTraversal(TreeNode *root) 20 { 21 /* 22 vector<int> rootVector; 23 vector<int> leftVector; 24 vector<int> rightVector; 25 26 if (root == NULL) 27 { 28 return rootVector; 29 } 30 31 leftVector = postorderTraversal(root->left); 32 rightVector = postorderTraversal(root->right); 33 rootVector.insert(rootVector.end(), leftVector.begin(), leftVector.end()); 34 rootVector.insert(rootVector.end(), rightVector.begin(), rightVector.end()); 35 rootVector.push_back(root->val); 36 37 return rootVector; 38 */ 39 40 vector<int> postorder; 41 stack<TreeNode *> s; 42 TreeNode *pre = NULL; 43 44 while (root || !s.empty()) 45 { 46 while (root) 47 { 48 s.push(root); 49 root = root->left; 50 } 51 52 root = s.top(); 53 if (root->right == NULL || pre == root->right) 54 { 55 postorder.push_back(root->val); 56 s.pop(); 57 pre = root; 58 root = NULL; 59 } 60 else 61 { 62 root = root->right; 63 } 64 } 65 66 return postorder; 67 } 68 }; 69 70 int main() 71 { 72 Solution s; 73 TreeNode *root = new TreeNode(1); 74 root->right = new TreeNode(2); 75 root->right->left = new TreeNode(3); 76 77 vector<int> v = s.postorderTraversal(root); 78 79 for (vector<int>::iterator it = v.begin(); it != v.end(); ++it) 80 { 81 cout << *it << endl; 82 } 83 84 system("pause"); 85 86 return 0; 87 }
递归的算法非常简单,就不说了
非递归的算法,前序和中序相对简单,主要就是利用栈来模拟递归,后序的思路是这样的,当前节点的右子树为空或者右子树已访问过,则访问该节点,否则访问该节点的右子树,这里需要有一个pre指针来标识前一个访问的节点是否为右子节点,链接:http://blog.csdn.net/chenqiumiao/article/details/6901309