BFS based C++ solution


  • 0
    S
    typedef TreeNode node;
    
    class Solution {
    public:
        int findBottomLeftValue(TreeNode* root) {
            int ret;
            
            queue<node *> q;
            q.push(root);
            bool cont = true;
            int sz = 1;
            
            while(!q.empty() && cont) {
                ret = q.front()->val;
                
                cont = false;
                while(sz--) {
                    node *n = q.front();
                    q.pop();
                    
                    if((n->left || n->right) && !cont)
                        cont = true;
                    
                    if(n->left)
                        q.push(n->left);
                    if(n->right)
                        q.push(n->right);
                }
                sz = q.size();
            }
            
            return ret;
        }
    };
    

Log in to reply
 

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