Binary Tree Preorder Traversal Problem & Solution

Given the root of a binary tree, return the preorder traversal of its nodes' values.

See the binary tree preorder traversal problem on LeetCode.

C++ Solution

#pragma GCC optimize("Ofast")
#pragma GCC optimization("max-inline-insns-recursive-auto")

static const int _=[](){std::ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);return 0;}();

/**
 * struct TreeNode {
 *   int val;
 *   TreeNode *left;
 *   TreeNode *right;
 *   TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *   TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *   TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
  vector<int> preorderTraversal(TreeNode* root) {
    vector<int> traversal;
    
    preorderTraversal(root, traversal);
    
    return traversal;
  }

private:
  void preorderTraversal(TreeNode* node, vector<int>& traversal) {
    if (node == nullptr) {
      return;
    }
    
    traversal.push_back(node->val);
    preorderTraversal(node->left, traversal);
    preorderTraversal(node->right, traversal);
  }
};

Are you looking for a job?