leetCode #94 Binary Tree Inorder Traversal

来源:互联网 发布:用什么看网络电视免费 编辑:程序博客网 时间:2024/06/04 19:40

题目:二叉树中序遍历

分析:也是递归

答案:

/** * 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 {private:    vector<int> res;public:    vector<int> inorderTraversal(TreeNode* root) {        dfs(root);        return res;    }        void dfs(TreeNode* root){        if (root!=NULL){            dfs(root->left);            res.push_back(root->val);            dfs(root->right);        }    }    };


0 0
原创粉丝点击