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

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

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

 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

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

时间: 2024-12-26 21:19:12

leetcode - Binary Tree Preorder Traversal && Binary Tree Inorder Traversal && Binary Tree Postorder Traversal的相关文章

[LeetCode] Binary Tree Preorder/Inorder/Postorder Traversal

前中后遍历 递归版 1 /* Recursive solution */ 2 class Solution { 3 public: 4 vector<int> preorderTraversal(TreeNode *root) { 5 6 vector<int> result; 7 preorderTraversal(root, result); 8 9 return result; 10 } 11 12 void preorderTraversal(TreeNode *root,

Leetcode | Construct Binary Tree from Inorder and (Preorder or Postorder) Traversal

Construct Binary Tree from Preorder and Inorder Traversal Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 递归构建. 思路就是: preorder可以定位到根结点,inorder可以定位左右子树的取值范围. 1. 由

[Leetcode][JAVA] Binary Tree Preorder Traversal, Binary Tree Inorder Traversal, Binary Tree Postorder Traversal

Binary Tree PreOrder Traversal: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3   return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 不使用递归前序遍历,可以

leetCode(18):Construct Binary Tree from Preorder and Inorder (Inorder and Postorder) Traversal

Given preorder and inorder (Inorder and Postorder) traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 前序和后序的特点是根结点要么在最前面.要么在最后面.已知根结点后,以根结点为界将中序遍历的结果分成两段,然后递归即可还原二叉树.有两点需要注意:首先二叉树中不能有重复的结点:其

【LeetCode】105 &amp; 106 Construct Binary Tree from (Preorder and Inorder) || (Inorder and Postorder)Traversal

Description: Given arrays recording 'Preorder and Inorder' Traversal (Problem 105) or  'Inorder and Postorder' (Problem 106), u need build the binary tree. Input: 105. Preorder & Inorder traversal 106. Inorder & Postorder traversal output: A binar

LeetCode之“树”:Binary Tree Preorder Traversal &amp;&amp; Binary Tree Inorder Traversal &amp;&amp; Binary Tree Postorder Traversal

Binary Tree Preorder Traversal 题目链接 题目要求: Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 递归解

LeetCode[Tree]: Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 这个题目的解法与LeetCode[Tree]: Construct Binary Tree from Preorder and Inorder Traversal的解法几乎相差无几.我的C++代码实现如下: class S

36. Construct Binary Tree from Inorder and Postorder Traversal &amp;&amp; Construct Binary Tree from Preorder and Inorder Traversal

Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assu

leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp; Construct Binary Tree f

1.  Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 代码: class Solution { public: TreeNode *buildTr