赞
踩
层次遍历
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: vector<int> levelOrder(TreeNode* root) { queue<TreeNode*> que; vector<int> res; que.push(root); if(!root) return res; TreeNode* temp; while (!que.empty()) { temp = que.front(); que.pop(); res.push_back(temp->val); if(temp->left) que.push(temp->left); if(temp->right) que.push(temp->right); } return res; } };
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。