c++ dfs solution.


  • 0
    class Solution {
    public:
      vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> ret;
        vector<TreeNode*> nodes;
    
        if (root != NULL)
        {   
          nodes.push_back(root);
          dfs(nodes, ret);
        }  
        return ret;
      }
    
      void dfs(vector<TreeNode*> &nodes, vector<vector<int>> &ret)
      {
        vector<int> vals;
        vector<TreeNode*> childNodes;
        if (nodes.size() == 0)
        {
          return;
        }  
        for (int i = 0; i < nodes.size(); i ++)
        {
          if (nodes[i] != NULL)
          {
    	vals.push_back(nodes[i] -> val);
            if (nodes[i] -> left != NULL)
    	{  
              childNodes.push_back(nodes[i] -> left);
    	}
    	if (nodes[i] -> right != NULL)
    	{
    	  childNodes.push_back(nodes[i] -> right);
    	}  
          }	
        }
        ret.push_back(vals);
        dfs(childNodes, ret);
      }
    };
    

Log in to reply
 

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