Title:
Given a binary tree, return the level order traversal of its nodes‘ values. (ie, from left to right, level by level).
For example:
Given binary tree {3,9,20,#,#,15,7}
,
3 / 9 20 / 15 7
return its level order traversal as:
[ [3], [9,20], [15,7] ] 思路:使用队列,并且每次记录下每层的数目,这样从队列中取元素也就清楚了每层的元素
/** * 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<vector<int>> levelOrder(TreeNode* root) { queue<TreeNode*> Q; vector<vector<int> > results; if (!root) return results; vector<int> result; Q.push(root); int count_pre = 1; int count_cur = 0; while (!Q.empty()){ TreeNode* top = Q.front(); result.push_back(top->val); Q.pop(); count_pre--; if (top->left){ Q.push(top->left); count_cur++; } if (top->right){ Q.push(top->right); count_cur++; } if (count_pre == 0){ count_pre = count_cur; count_cur = 0; results.push_back(result); result.clear(); } } return results; } };
还可以使用递归。关键是要保存到对应的层数
/** * 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<vector<int>> levelOrder(TreeNode* root) { vector<vector<int> > result; travel(root,result,1); return result; } void travel(TreeNode* root,vector<vector<int> > &result,int level){ if (!root) return ; if (level > result.size()) result.push_back(vector<int> ()); result[level-1].push_back(root->val); travel(root->left,result,level+1); travel(root->right,result,level+1); } };
Title:
Given a binary tree, return the zigzag level order traversal of its nodes‘ values. (ie, from left to right, then right to left for the next level and alternate between).
For example:
Given binary tree {3,9,20,#,#,15,7}
,
3 / 9 20 / 15 7
return its zigzag level order traversal as:
[ [3], [20,9], [15,7] ]一样的思路啊!如果不使用递归,则使用一个标记,看这层是否反转,直接使用reverse函数即可。如果使用递归,则也是在相应的层反转。但是因为是在插入的过程中,所以应该是插入每层的vector头。
/** * 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<vector<int>> zigzagLevelOrder(TreeNode* root) { vector<vector<int> > results; travel(root,1,results,true); return results; } void travel(TreeNode* root, int level, vector<vector<int> > &results, bool left_to_right){ if (!root) return; if (level > results.size()) results.push_back(vector<int>()); if (left_to_right) results[level-1].push_back(root->val); else results[level-1].insert(results[level-1].begin(),root->val); travel(root->left,level+1,results,!left_to_right); travel(root->right,level+1,results,!left_to_right); } };
/** * 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<vector<int>> zigzagLevelOrder(TreeNode* root) { vector<vector<int> > results; travel(root,1,results,true); return results; } void travel(TreeNode* root, int level, vector<vector<int> > &results, bool left_to_right){ if (!root) return; if (level > results.size()) results.push_back(vector<int>()); if (left_to_right) results[level-1].push_back(root->val); else results[level-1].insert(results[level-1].begin(),root->val); travel(root->left,level+1,results,!left_to_right); travel(root->right,level+1,results,!left_to_right); } };
时间: 2024-10-11 20:08:43