Classical algorithm preorder traversal binary tree

1. Topic description
Given a binary tree, return its preorder traversal.

Example:

Input: [. 1, null, 2,3]
. 1

2
/
. 3

Output: [1,2,3]
Advanced: The recursive algorithm is very simple, can you complete iterative algorithm?

2. My code
/ **

  • Definition for a binary tree node.

  • struct TreeNode {

  • int val;
    
  • TreeNode *left;
    
  • TreeNode *right;
    
  • TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    
  • };
    /
    class Solution {
    public:
    vector preorderTraversal(TreeNode
    root) {
    TreeNode* cur_node;
    if(root == NULL)
    return result_vec;

     result_vec.push_back(root->val);
     preorderTraversal(root->left);
     preorderTraversal(root->right);
    
     return result_vec;
    

    }
    vector result_vec;
    };

Use stack
/ **

  • Definition for a binary tree node.

  • struct TreeNode {

  • int val;
    
  • TreeNode *left;
    
  • TreeNode *right;
    
  • TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    
  • };
    /
    class Solution {
    public:
    vector preorderTraversal(TreeNode
    root) {
    TreeNode* cur_node;
    stack<TreeNode*> stack_node;
    TreeNode* pre_node;

     if(root == NULL)
         return result_vec;
     cur_node = root;
     while(cur_node != NULL || !stack_node.empty()){
         if(cur_node == NULL){
             cur_node = stack_node.top();
             stack_node.pop();
         }
         result_vec.push_back(cur_node->val);
         if(cur_node->right != NULL)
             stack_node.push(cur_node->right);
         cur_node = cur_node->left;
     };
    
     return result_vec;
    

    }
    vector result_vec;
    };
    3. A better solution online
    Morris algorithm logic is not clear, but it may save storage space
    4. Where it can be improved
    a. Using the stack method, the execution efficiency is not high, and the logic design is wrong
    b.
    5. Optimize the code to be incomprehensible (efficiency defeat: 63%, memory defeat: 100%)
    / **

  • Definition for a binary tree node.

  • struct TreeNode {

  • int val;
    
  • TreeNode *left;
    
  • TreeNode *right;
    
  • TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    
  • };
    /
    class Solution {
    public:
    vector preorderTraversal(TreeNode
    root) {
    TreeNode* cur_node;
    stack<TreeNode*> stack_node;
    TreeNode* pre_node;

     if(root == NULL)
         return result_vec;
     cur_node = root;
     stack_node.push(cur_node);
     while(!stack_node.empty()){
         cur_node = stack_node.top();
         stack_node.pop();
         result_vec.push_back(cur_node->val);
         if(cur_node->right != NULL)
             stack_node.push(cur_node->right);
         if(cur_node->left != NULL)
             stack_node.push(cur_node->left);
     };
    
     return result_vec;
    

    }
    vector result_vec;
    };
    6. Thinking obtained When the
    traversal order is different, the logic of entering the stack is different, otherwise it will affect the logic clarity and even the execution efficiency

Published 4 original articles · Likes0 · Visits 22

Guess you like

Origin blog.csdn.net/digitaluser/article/details/105617352