2017年4月9日21:37:54 145. Binary Tree Postorder Traversal 【】hard

来源:互联网 发布:梦三国矩阵密保卡下载 编辑:程序博客网 时间:2024/06/16 21:53

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

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

   1    \     2    /   3

return [3,2,1].

hard题中很简单的二叉树遍历 

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

一个函数存遍历的数,一个函数遍历 用引用传递保留数据

0 0
原创粉丝点击