Leetcode 94. Binary Tree Inorder Traversal

 

 中序遍历,常规操作

/**
 * 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> v;
        inorder(v,root);
        return v;
    }
    void inorder(vector<int>& v, TreeNode* root) {
        if(!root) return;
        inorder(v, root->left);
        v.push_back(root->val);
        inorder(v, root->right);
    }
};

 

posted @ 2020-02-07 03:33  SteveYu  阅读(94)  评论(0编辑  收藏  举报