码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode - Binary Tree Preorder Traversal && Binary Tree Inorder Traversal && Binary Tree Postorder Traversal

时间:2014-06-17 12:53:42      阅读:416      评论:0      收藏:0      [点我收藏+]

标签:des   style   class   blog   code   http   

简单来说,就是二叉树的前序、中序、后序遍历,包括了递归和非递归的方法

 

前序遍历(注释中的为递归版本):

bubuko.com,布布扣
 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 }
View Code

中序遍历(注释中的为递归版本):

bubuko.com,布布扣
 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 }
View Code

后序遍历(注释中的为递归版本):

bubuko.com,布布扣
 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 }
View Code

 

递归的算法非常简单,就不说了

非递归的算法,前序和中序相对简单,主要就是利用栈来模拟递归,后序的思路是这样的,当前节点的右子树为空或者右子树已访问过,则访问该节点,否则访问该节点的右子树,这里需要有一个pre指针来标识前一个访问的节点是否为右子节点,链接:http://blog.csdn.net/chenqiumiao/article/details/6901309

leetcode - Binary Tree Preorder Traversal && Binary Tree Inorder Traversal && Binary Tree Postorder Traversal,布布扣,bubuko.com

leetcode - Binary Tree Preorder Traversal && Binary Tree Inorder Traversal && Binary Tree Postorder Traversal

标签:des   style   class   blog   code   http   

原文地址:http://www.cnblogs.com/laihaiteng/p/3791760.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!