Binary Tree inorder/preorder traversal

Source: Internet
Author: User

Given a binary tree, the system returns all elements traversed in ascending or descending order of a set.

There are two methods: one is the classic Recursive Method of the classic middle-order/first-order method, and the other can be combined with the stack to implement non-recursive

 

Given a binary tree, returnInorderTraversal of its nodes 'values.

For example:
Given Binary Tree{1,#,2,3},

   1         2    /   3

 

Return[1,3,2].


OJ's binary tree serialization:

The serialization of a binary tree follows a level order traversal, where '# 'signiies a path Terminator where no node exists below.

Here's an example:

   1  /  2   3    /   4         5
The above binary tree is serialized "{1,2,3,#,#,4,#,#,5}".
 1 /** 2  * Definition for binary tree 3  * struct TreeNode { 4  *     int val; 5  *     TreeNode *left; 6  *     TreeNode *right; 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8  * }; 9  */10 class Solution {11 public:12     vector<int> inorderTraversal(TreeNode *root) {13         vector<int> ret;14         if(root == NULL)15             return ret;16             17         stack<TreeNode *> stack;18         stack.push(root);19         20         while(!stack.empty()){21             TreeNode *node = stack.top();22             stack.pop();23             if(node->left == NULL && node->right == NULL){24                 ret.push_back(node->val);25             }26             else{27                 if(node->right != NULL)28                     stack.push(node->right);29                 stack.push(node);30                 if(node->left != NULL)31                     stack.push(node->left);32                     33                 node->left = node->right = NULL;34             }35             36         }37         38         return ret;39             40     }41 };

 

Given a binary tree, returnPreorderTraversal of its nodes 'values.

For example:
Given Binary Tree{1,#,2,3},

   1         2    /   3

 

Return[1,2,3].

As required above, only elements in a sequence in the middle order are returned. This time, Recursive Implementation is used:

 1 /** 2  * Definition for binary tree 3  * struct TreeNode { 4  *     int val; 5  *     TreeNode *left; 6  *     TreeNode *right; 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8  * }; 9  */10 class Solution {11 public:12     vector<int> preorderTraversal(TreeNode *root) {13         vector<int> ret;14         if(root == NULL)15             return ret;16         PreorderTraversal(root,ret);17         return ret;18     }19     20     void PreorderTraversal(TreeNode *root,vector<int> &ret){21         if(root != NULL){22             ret.push_back(root->val);23             PreorderTraversal(root->left,ret);24             PreorderTraversal(root->right,ret);25         }26     }27 };

 

Binary Tree inorder/preorder traversal

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.