LeetCode | Binary Tree Level Order Traversal II
来源:互联网 发布:nlp课程的危害 知乎 编辑:程序博客网 时间:2024/06/05 08:03
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree {3,9,20,#,#,15,7}
,
3 / \ 9 20 / \ 15 7
return its bottom-up level order traversal as:
[ [15,7], [9,20], [3]]
confused what "{1,#,2,3}"
means? > read
思路:与Binary Tree Level Order Traversal这题一致,就是将最后的结果倒置一下即可。
class Solution {public: vector<vector<int> > levelOrderBottom(TreeNode *root) {queue<TreeNode*> L;TreeNode* temp;vector<int>v;vector<TreeNode*>vt;vector<vector<int> >vv;if(root){v.push_back(root->val);vv.push_back(v);}L.push(root);while(!L.empty()){temp = L.front();vt.push_back(temp);L.pop();if(!temp)continue;L.push(temp->left);L.push(temp->right);}int i,j,cnt=0;int step = 3;for(i=1; i<vt.size(); i=j){v.clear();for(j=i; j<step && j<vt.size(); j++){if(vt[j]!=NULL){v.push_back(vt[j]->val);cnt+=2;}}step = j+cnt;cnt = 0;if(j<vt.size())vv.push_back(v);}reverse(vv.begin(), vv.end());//结果倒置 return vv; }};
0 0
- LeetCode: Binary Tree Level Order Traversal II
- [LeetCode]Binary Tree Level Order Traversal II
- LeetCode Binary Tree Level Order Traversal II
- [Leetcode] Binary Tree Level Order Traversal II
- LeetCode Binary Tree Level Order Traversal II
- [LeetCode] Binary Tree Level Order Traversal II
- 【leetcode】Binary Tree Level Order Traversal II
- [LeetCode]Binary Tree Level Order Traversal II
- [Leetcode]Binary Tree Level Order Traversal II
- [leetcode]Binary Tree Level Order Traversal II
- LeetCode-Binary Tree Level Order Traversal II
- LeetCode - Binary Tree Level Order Traversal II
- [LeetCode] Binary Tree Level Order Traversal II
- LeetCode | Binary Tree Level Order Traversal II
- LeetCode - Binary Tree Level Order Traversal II
- 【leetcode】Binary Tree Level Order Traversal II
- 【LeetCode】Binary Tree Level Order Traversal II
- [LeetCode] Binary Tree Level Order Traversal II
- linux makefile详解
- 关于hibernate4.3.8final版本(annotation)注解问题 相当的bug啊 求大神指点迷津
- P51-16
- 解决Fetching android sdk component information加载过久问题
- 第四周总结
- LeetCode | Binary Tree Level Order Traversal II
- Android自定义控件_自定义属性format详解
- 求给定序列前N项和之二
- android 开发实现静默安装
- 传输控制协议(TCP)
- 第五周——体验常成员函数——坐标类
- 三层架构:表示层——业务逻辑层——数据访问层
- struts2 action的作用域
- 用c语言实现 判断一个字符串是不是回文字符串