Binary tree traversal (recursive, non-recursive)

1. Recursive traversal

1 void helper(TreeNode* root, vector<int>& res) {
2         if (root) {
3             res.push_back(root->val);
4             helper(root->left, res);
5             helper(root->right, res);
6         }
7 }

Swap rows 3 to 5 in the order of access to achieve pre-order traversal, in-order traversal, and post-order traversal.

 

2. Non-recursive traversal

Non-recursive traversal can be implemented using stacks.

a. Preorder traversal (144. Binary Tree Preorder Traversal)

Preorder traversal node access order is root-left-right

So for each node that is popped, the stacking order is the right child first, then the left child

 1 class Solution {
 2 public:
 3     vector<int> preorderTraversal(TreeNode* root) {
 4         vector<int> res;
 5         stack<TreeNode*> stk;
 6         if(root)
 7             stk.push(root);
 8         while (!stk.empty()) {
 9             TreeNode *temp = stk.top();
10             stk.pop();
11             res.push_back(temp->val);
12             if (temp->right)
13                 stk.push(temp->right);
14             if (temp->left)
15                 stk.push(temp->left);
16         }
17         return res;
18     }
19 };

b. Inorder Traversal (94. Binary Tree Inorder Traversal)

Inorder traversal node access order is left-root-right

Initialization, temp points to root

1. temp is not empty, temp is pushed onto the stack, temp = temp -> left

2. If temp is empty, access the top node of the stack and pop it from the stack, temp = temp -> right

Because it is pushed to the stack first, the pointer can never point back to the top of the stack during the popping process.

 1 class Solution {
 2 public:
 3     vector<int> inorderTraversal(TreeNode* root) {
 4         vector<int> res;
 5         stack<TreeNode*> stk;
 6         TreeNode *temp = root;
 7         while(!stk.empty() || temp) {
 8             if (temp) {
 9                 stk.push(temp);
10                 temp = temp->left;
11             }  
12             else {
13                 temp = stk.top();
14                 stk.pop();
15                 res.push_back(temp->val);
16                 temp = temp->right;
17             }
18         }
19         return res;
20     }
21 };

c. Postorder Traversal (145. Binary Tree Postorder Traversal)

Postorder traversal node access order is left-right-root

When traversing in the form of root-right-left, the order after popping the stack is just reverse post-order traversal. The operation is the same as the preorder traversal, but this time the left child is pushed into the stack first.

You can push the popped node into another stack, then the order of nodes exiting from stack 2 is post-order traversal.

 1 class Solution {
 2 public:
 3     vector<int> postorderTraversal(TreeNode* root) {
 4         vector<int> res;
 5         stack<TreeNode*> stk1;
 6         stack<TreeNode*> stk2;
 7         if(root)
 8             stk1.push(root);
 9         while(!stk1.empty()){
10             TreeNode *temp = stk1.top();
11             stk2.push(temp);
12             stk1.pop();
13             if(temp->left)
14                 stk1.push(temp->left);
15             if(temp->right)
16                 stk1.push(temp->right);
17         }
18         while(!stk2.empty()) {
19             res.push_back(stk2.top()->val);
20             stk2.pop();
21         }
22         return res;
23     }
24 };

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325838975&siteId=291194637