NO.94 preorder

Given a binary tree, its return in order  traversal.

Example:

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

Output: [1,3,2]
/**
 * 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<int> inorderTraversal(TreeNode* root) {
        vector<int> ret;
        if(!root)return ret;
        ret=inorderTraversal(root->left);
        ret.push_back(root->val);
        vector<int> tmp(inorderTraversal(root->right));
        ret.insert(ret.end(),tmp.begin(),tmp.end());
        return ret;
    }
};

When execution: 4 ms, beat the 96.39% of users in the Binary Tree Inorder Traversal of C ++ submission

Memory consumption: 10.6 MB, beat the 9.33% of users in the Binary Tree Inorder Traversal of C ++ submission

Guess you like

Origin blog.csdn.net/xuyuanwang19931014/article/details/91044223