lintcode-二叉树的层次遍历 II
2017-11-11 19:58
471 查看
描述
给出一棵二叉树,返回其节点值从底向上的层次序遍历(按从叶节点所在层到根节点所在的层遍历,然后逐层从左往右遍历)样例
给出一棵二叉树 {3,9,20,#,#,15,7},3 / \ 9 20 / \ 15 7
按照从下往上的层次遍历为:
[ [15,7], [9,20], [3] ]
代码
/** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this->right = NULL; * } * } */ #include<queue> #include<algorithm> class Solution { public: /* * @param root: A tree * @return: buttom-up level order a list of lists of integer */ vector<vector<int>> levelOrderBottom(TreeNode * root) { // write your code here TreeNode * q=root; vector<vector<int>> res; if(root==NULL){ return res; } queue<TreeNode *> childs; childs.push(q); TreeNode * tt=NULL; int len=1; while(!childs.empty()){ vector<int> temp; len=childs.size(); while(len--){ temp.push_back(childs.front()->val); tt=childs.front(); childs.pop(); if(tt->left!=NULL){ childs.push(tt->left); } if(tt->right!=NULL){ childs.push(tt->right); } } res.push_back(temp); } reverse(res.begin(),res.end()); return res; } };
相关文章推荐
- 二叉树的层次遍历 II,lintcode
- LintCode 二叉树的层次遍历 II
- lintcode : 二叉树的层次遍历II
- LintCode笔记(4)——二叉树的层次遍历II
- LintCode 70 二叉树的层次遍历 II
- LintCode 二叉树的层次遍历 II
- 二叉树的层次遍历 II -LintCode
- Lintcode 二叉树的层次遍历 II
- LintCode-二叉树的层次遍历 II
- [Lintcode] #70 二叉树的层次遍历 II
- lintcode-二叉树的层次遍历II-70
- [Lintcode]Binary Tree Level Order Traversal II 二叉树的层次遍历 II
- LintCode-69.二叉树的层次遍历
- LintCode 二叉树的层次遍历
- 70 - 二叉树的层次遍历 II
- 二叉树的层次遍历,lintcode
- 二叉树层次遍历-LintCode
- LintCode 69 二叉树的层次遍历
- 二叉树的层次遍历 II
- LintCode:二叉树的层次遍历