Simple cpp bfs 9ms


  • 0
    Z
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    #include <vector>
    class Solution {
    public:
        int findBottomLeftValue(TreeNode* root) {
            std::vector<TreeNode *> v({root});
            TreeNode *bln;
            while (!v.empty()) {
                bln = v[0];
                std::vector<TreeNode *> new_v(v);
                v = std::vector<TreeNode *> ();
                for (auto node: new_v) {
                    if (node->left != NULL) {
                        v.push_back(node->left);
                    }
                    if (node->right != NULL) {
                        v.push_back(node->right);
                    }
                }
            }
            return bln->val;
        }
    };
    

Log in to reply
 

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