passed 211/212


  • 0
    S

    '''
    public:
    struct Element
    {
    int val;
    int dir;
    int height;
    Element(int _val, int _dir, int _height)
    {
    val = _val;
    dir = _dir;
    height = _height;
    }
    bool operator < (Element b)
    {
    return dir < b.dir || dir == b.dir && height < b.height;
    }
    };
    vector<Element> array;
    void dfs(TreeNode * node, int dir,int height)
    {
    if (node == NULL) return ;
    array.push_back(Element(node->val,dir,height));
    dfs(node->left,dir-1,height+1);
    dfs(node->right,dir+1,height+1);
    }
    vector<vector<int>> verticalOrder(TreeNode* root) {
    dfs(root,0,0);
    vector<vector<int>> res;
    if (root==NULL) return res;
    set<int> s;
    sort(array.begin(),array.end());
    for (auto p : array)
    {

            if (s.find(p.dir)==s.end()) 
            {
                s.insert(p.dir);
                res.push_back(vector<int>());
            }
                           
            res[s.size()-1].push_back(p.val);
            
        }
        return res;
        
    }
    

    '''


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.