代码随想录算法训练营第十二天| 递归遍历 (必须掌握)迭代遍历 统一迭代

递归遍历

重点:

1,TreeNode的自定义

2,val =0 ==  val = NULL;

代码:

 1 void preRecursor(TreeNode* root, vector<int>& result)
 2 {
 3     if (root == NULL)
 4         return;
 5     result.push_back(root->val);
 6     preRecursor(root->left, result);
 7     preRecursor(root->right, result);
 8 }
 9 
10 vector<int> preorderTraversal(TreeNode* root)
11 {
12     vector<int> result;
13     preRecursor(root, result);
14     return result;
15 }

 迭代遍历

用stack进行遍历,较为简单,直接贴代码:

 1 // 使用迭代法,进行遍历
 2 //前序遍历 中左右
 3 //思考,用栈来存放,先放中,然后出来,放右,左,在对左出来,放它的右左
 4 vector<int> preorderTraversal_iterator(TreeNode* root)
 5 {
 6     vector<int> result;
 7     //很重要:
 8     if (root == NULL) return result;
 9     stack<TreeNode*> selected;
10     selected.push(root);
11     while (!selected.empty())
12     {
13         auto curTop = selected.top();
14         selected.pop();
15         result.push_back(curTop->val);
16 
17         //如果NULL,那么放到stack里面会怎么样?
18         if(curTop->right != NULL)
19             selected.push(curTop->right);
20         if (curTop->left != NULL)
21             selected.push(curTop->left);
22     }
23 
24     return result;
25 }
26 
27 //后续遍历  中左右 -》 左右中   中右左,在反转?
28 vector<int> postTraversal_iterator(TreeNode* root)
29 {
30     vector<int> result;
31     stack<TreeNode*> selected;
32     if (root == NULL) return result;
33     selected.push(root);
34     while (!selected.empty())
35     {
36         auto top_ = selected.top();
37         selected.pop();
38         result.push_back(top_->val);
39         if (top_->left != NULL) selected.push(top_->left);
40         if (top_->right != NULL) selected.push(top_->right);
41     }
42 
43     reverse(result.begin(), result.end());
44 
45     return result;
46 }
47 
48 
49 //中序遍历 左中右
50 // 中,暂时不出,遍历到 没有左孩子的节点,弹出,然后再看这个节点的下一个节点,右孩子,弹出,再看下面的
51 //重要思路是,一路向左
52 vector<int> inorderTraversal_iterator(TreeNode* root)
53 {
54     vector<int> result;
55     if (root == NULL) return result;
56 
57     stack<TreeNode*> selected;
58     selected.push(root);
59     while (!selected.empty())
60     {
61         // 应该遍历过的就 删掉,不要了
62         auto top_ = selected.top();
63         if (top_->left != NULL)
64         {
65             selected.push(top_->left);
66             top_->left = NULL;
67         }
68         else
69         {
70             result.push_back(top_->val);
71             selected.pop();
72 
73             if (top_->right != NULL)
74                 selected.push(top_->right);
75         }
76     }
77 
78     return result;
79 }

 用指针进行遍历的中序遍历很有意思,需要好好看看

代码:

 1 vector<int> inorderTraversal_iterator2(TreeNode* root)
 2 {
 3     vector<int> result;
 4     if (root == NULL) return result;
 5 
 6     stack<TreeNode*> selected;
 7     TreeNode* cur = root;
 8     //让cur 可能指向叶子的空节点,因为这里需要想怎么把cur 进行传递
 9     while (cur!=NULL || !selected.empty())
10     {
11         if (cur != NULL)
12         {
13             selected.push(cur);
14             cur = cur->left;
15         }
16         else
17         {
18             cur = selected.top();
19             selected.pop();
20             result.push_back(cur->val);
21             cur = cur->right;
22         }
23     }
24     return result;
25 }

 

posted @ 2023-06-20 10:23  博二爷  阅读(13)  评论(0编辑  收藏  举报