6ms C++ solution


  • 0
    E
    class Solution {
    public:
    	struct QueueNode {
    		TreeNode* val;
    		int depth;
    		QueueNode(TreeNode *x, int y) :val(x), depth(y) {}
    	};
        int maxDepth(TreeNode* root) {
    		if(root == NULL)
    			return 0;
    		queue<QueueNode> Q;
    		QueueNode queueRoot(root,1);
    		Q.push(queueRoot);
    		int maxDepth = 0;
    		while (Q.size() > 0)
    		{
    			QueueNode tmp = Q.front();
    			Q.pop();
    			if (maxDepth < tmp.depth)
    				maxDepth = tmp.depth;
    			if (tmp.val->left != NULL)
    				Q.push(QueueNode(tmp.val->left,tmp.depth+1));
    			if (tmp.val->right != NULL)
    				Q.push(QueueNode(tmp.val->right, tmp.depth + 1));
    		}
    		return maxDepth;
        }
    };
    

Log in to reply
 

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