Leetcode: Binary Tree Inorder Traversal

来源:互联网 发布:大数据盈利模式 编辑:程序博客网 时间:2024/05/29 18:47

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].

/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:    void inorder(TreeNode* root, vector<int> &path)    {        if(root != NULL)        {            inorder(root->left, path);            path.push_back(root->val);            inorder(root->right, path);        }    }    vector<int> inorderTraversal(TreeNode *root) {        // Start typing your C/C++ solution below        // DO NOT write int main() function        vector<int> path;        inorder(root, path);        return path;    }};





原创粉丝点击