中序遍历二叉树

vector<int> inorderTraversal(TreeNode* root) {
    vector<int> inorderTraversalElems;
    
    function<void(TreeNode*& node)> traversal;
    traversal = [&](TreeNode*& node)
    {
        if (node == nullptr) {
            return;
        }
        
        traversal(node->left);
        inorderTraversalElems.push_back(node->val);
        traversal(node->right);
    };
    
    traversal(root);
    return inorderTraversalElems;
}

 

posted @ 2015-08-12 07:23  wu_overflow  阅读(173)  评论(0编辑  收藏  举报