C++ single queue, iterative solution with simple BFS


  • 0
    S
    std::vector<std::vector<int> > levelOrder(TreeNode* root)
    {
      std::vector<std::vector<int> > returnValues;
      if (root==NULL) return returnValues;
      std::queue<TreeNode*> nodesQueue;
      TreeNode* currNode = root;
      int totalNodesAtcurrHeight = 1;
      nodesQueue.emplace(root);
      do
      {
        std::vector<int> currLevelValues;
        for (unsigned int i = 0; i < totalNodesAtcurrHeight; i++)
        {
          currNode = nodesQueue.front();
          nodesQueue.pop();
          currLevelValues.emplace_back(currNode->val);
          bool hasRightNode = (currNode->right != NULL);
          bool hasLeftNode  = (currNode->left  != NULL);
          if (hasLeftNode)  nodesQueue.emplace(currNode->left);
          if (hasRightNode) nodesQueue.emplace(currNode->right);
        }
        returnValues.emplace_back(currLevelValues);
        totalNodesAtcurrHeight = nodesQueue.size();
      } while(!nodesQueue.empty());
      return returnValues;
    }
    

Log in to reply
 

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