LeetCode - Binary Tree Inorder Traversal

来源:互联网 发布:非法网络传销罪 编辑:程序博客网 时间:2024/05/13 12:54

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

For example:
Given binary tree {1,#,2,3},

   1    \     2    /   3

return [1,3,2].

Note: Recursive solution is trivial, could you do it iteratively?

vector<int> inorderTraversal(TreeNode *root) {    // Start typing your C/C++ solution below    // DO NOT write int main() function    vector<int> v;    if (NULL == root) {        return v;    }    vector<int> l = inorderTraversal(root->left);    for (int i = 0; i < l.size(); ++i) {        v.push_back(l[i]);    }    v.push_back(root->val);    vector<int> r = inorderTraversal(root->right);    for (int i = 0; i < r.size(); ++i) {        v.push_back(r[i]);    }    return v;}


原创粉丝点击