c++ solution


  • 0
    W
    class Solution {
    public:
        int findBottomLeftValue(TreeNode* root) {
            queue<TreeNode*> q;
    		q.push(root);
    		TreeNode* curleft=root;
    		while (!q.empty()) {
    			int len = q.size();
    			TreeNode* newleft=NULL;
    			for (int i=0; i<len; ++i) {
    				TreeNode* n=q.front();
    				q.pop();
    				if (n->left) {
    					q.push(n->left);
    					if (newleft==NULL) newleft=n->left;
    				}
    				if (n->right) {
    					q.push(n->right);
    					if (newleft==NULL) newleft=n->right;
    				}
    			}
    			if (newleft) curleft=newleft;
    		}
    		return curleft->val;
        }
    };
    

Log in to reply
 

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