s4553711
4/14/2017 - 4:14 PM

515.cpp

/**
 * 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> result;
    vector<int> largestValues(TreeNode* root) {
        BFS(root, 0);
        return result;
    }
    
    void BFS(TreeNode* root, int dp) {
        if (root == NULL) return;
        
        if (dp == result.size()) 
            result.push_back(root->val);
        else
            if (result[dp] < root->val) {
                result[dp] = root->val;
            }
        
        BFS(root->left, dp+1);
        BFS(root->right, dp+1);
    }
    
};