Leetcode: Binary Tree preorder traversal (first-order traversal of a binary tree)

Source: Internet
Author: User

Question:

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

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

   1         2    /   3

 

Return[1,2,3].

Note:Recursive solution is trivial, cocould You Do It iteratively?

Note:

1) recursive and non-recursive implementations, including non-recursive Methods

2) Complexity, time O (N), Space O (N)

Implementation:

1. Recursion

 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> root_vec;14       vector<int> left_vec;15       vector<int> right_vec;16       if(root==NULL) return root_vec;17       root_vec.push_back(root->val);18       if(root->left!=NULL) left_vec=preorderTraversal(root->left);19       if(root->right!=NULL) right_vec=preorderTraversal(root->right);20       root_vec.insert(root_vec.end(),left_vec.begin(),left_vec.end());21       root_vec.insert(root_vec.end(),right_vec.begin(),right_vec.end());22       return root_vec;23     }24 };
View code

Ii. Non-recursion

 

In the order of sequential traversal, the root node is accessed first, then the left subtree is accessed, and then the right subtree is accessed. For each subtree, The traversal is performed in the same order, the implementation of non-recursion is as follows:

For any node P,

1) Output node P, and then import it into the stack. Then, check whether the left child of P is empty;

2) If the left child of P is not empty, set the left child of P to the current node. Repeat 1;

3) if the left child of P is empty, the top node of the stack is output but not the current node, check whether it is empty;

4) if it is not null, the operation is cyclically performed;

5) if it is null, the system continues to output the stack, but does not output the stack. At the same time, the right child of the outbound stack node is set to the current node to see if it is null. Repeat the steps 4) and 5) operation;

6) The traversal ends until the current node P is null and the stack is empty.

A. the following code is implemented normally, which is consistent with the above analysis process.

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> preorder_vec; 14 treenode * P = root; // define the pointer to the currently accessed node 15 if (P = NULL) return preorder_vec; // if it is an empty tree, return NULL vector16 stack <treenode *> T Reenode_stack; // create an empty stack 17 // when the current node P is null and the stack is empty, the loop ends at 18 while (p |! Treenode_stack.empty () 19 {20 // output the current node from the root node and add it to the stack. 21 // set its left child to the current node at the same time, until there is no left child, and the current node is null 22 preorder_vec.push_back (p-> Val); 23 treenode_stack.push (p); 24 p = p-> left; 25 // if the current node P is null and the stack is not empty, the top node of the stack will go out of the stack, 26 // set its right child to the current node at the same time, and Judge cyclically, until P is not blank 27 while (! P &&! Treenode_stack.empty () 28 {29 p = treenode_stack.top (); 30 treenode_stack.pop (); 31 P = p-> right; 32} 33} 34} 35 };
View code

B. the following code is concise (personal feeling, don't like it)

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 stack <treenode *> preorder_stack; 14 treenode * P = NULL; 15 vector <int> preorder_vec; 16 if (root = NULL) return preorder_vec; // if the tree is empty, null vector17 is returned. Preorder_stack.push (Root); // the current node is in the stack for 18 while (! Preorder_stack.empty () 19 {20 p = preorder_stack.top (); // The top node of the stack outputs the stack and 21 preorder_stack.pop (); 22 preorder_vec.push_back (p-> Val ); 23 // Note: The following stack entry sequence cannot be wrong, because first the right back to the left, 24 // in this way, the first traversal is the left child (left-> middle-> right) 25 if (p-> right) preorder_stack.push (p-> right); // if there is a right child, 26 if (p-> left) preorder_stack.push (p-> left); // stack 27} 28 return preorder_vec; 29} 30} if left child exists };
View code

 

 

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.