本题和上题一样同属于层次遍历,不同的是本题从底层往上遍历,如下:

代码如下:

 struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
TreeNode():val(),left(NULL),right(NULL){}
TreeNode(int x): val(x), left(NULL),right(NULL) {}
}; vector<vector<int> > levelOrderTraversalII(TreeNode *root) //非递归的中序遍历(用栈实现)
{ queue<TreeNode *> tree_queue;
vector<vector<int> > tree_vector, tree_rvector;
vector<int> svector;
int nCount = ; if (NULL == root) {
return tree_vector;
}
TreeNode *pTemp = root;
tree_queue.push(root);
tree_queue.push(NULL); //the end of one level. while (true) {
TreeNode *pTemp = tree_queue.front();
tree_queue.pop(); if (!pTemp) { //get the null, put vector<> to vector<vector<>>
tree_vector.push_back(svector);
nCount ++;
svector.clear();
if (tree_queue.empty())
break;
tree_queue.push(NULL);
}
else {
svector.push_back(pTemp->val);
if (pTemp->left)
tree_queue.push(pTemp->left);
if (pTemp->right)
tree_queue.push(pTemp->right);
}
}
while (nCount > ){
svector = tree_vector.at(nCount - );
tree_rvector.push_back(svector);
nCount --;
}
return tree_rvector;
}

LeetCode: 107_Binary Tree Level Order Traversal II | 二叉树自底向上的层次遍历 | Easy的更多相关文章

  1. [LeetCode] Binary Tree Level Order Traversal II 二叉树层序遍历之二

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  2. [Leetcode] Binary tree level order traversal ii二叉树层次遍历

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  3. LeetCode Binary Tree Level Order Traversal II (二叉树颠倒层序)

    题意:从左到右统计将同一层的值放在同一个容器vector中,要求上下颠倒,左右不颠倒. 思路:广搜逐层添加进来,最后再反转. /** * Definition for a binary tree no ...

  4. [LeetCode] 107. Binary Tree Level Order Traversal II 二叉树层序遍历 II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  5. [leetcode]Binary Tree Level Order Traversal II @ Python

    原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题意: Given a binary tree, ...

  6. LeetCode——Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  7. leetCode :103. Binary Tree Zigzag Level Order Traversal (swift) 二叉树Z字形层次遍历

    // 103. Binary Tree Zigzag Level Order Traversal // https://leetcode.com/problems/binary-tree-zigzag ...

  8. LeetCode - Binary Tree Level Order Traversal II

    题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...

  9. [LeetCode107]Binary Tree Level Order Traversal II 二叉树层次遍历

    题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...

随机推荐

  1. jstat查看JVM GC情况

    转自 https://www.cnblogs.com/yjd_hycf_space/p/7755633.html

  2. JSON转Excel

    1.引入js (dist目录下JsonExportExcel.min.js) <script src="https://cuikangjie.github.io/JsonExportE ...

  3. php生成红包

    <?php /** * 随机生成红包金额 * @param $n 红包个数 * @param $sum 总金额 整数 * @param $index_max 最大金额在数组中索引 * @para ...

  4. "Web Scraping with Python"笔记(一)

    1.  合法性:抓取的数据用于个人使用,不存在问题:数据用于转载,需注意抓取的数据类型. 一般情况,抓取的真实数据(营业地址,电话清单等)允许转载.而原创数据(比如意见和评论)受版权限制不能转载. 2 ...

  5. 浅谈js抽象工厂模式

    一.简单工厂 定义:简单工厂模式中,可以根据参数的不同返回不同类的实例.简单工厂模式专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类. 比如你去专门卖鼠标的地方你可以买各种各样的 ...

  6. Vimtutor中文版

    ================================================================================      欢     迎     阅  ...

  7. Windows下PythonQt3.2使用pandas.pivot_table

     本机环境 1.win7 64 旗舰版 2.Qt 5.9.1(MSVC 2015,32 bit) 3.Python 3.7.1 (32-bit),二进制包安装的,即Windows x86 execut ...

  8. dfs | Security Badges

    Description You are in charge of the security for a large building, with n rooms and m doors between ...

  9. SSRF绕过filter_var(),preg_match()和parse_url()

    1.利用curl的变量解释符$ php版本 利用代码 /*ssrf.php*/<?php echo ]."n"; // check if argument is a vali ...

  10. java多线程系列16 线程池

    当系统系统规模较小,我们可以不使用线程池.但是当系统到达一定规模,频繁的创建和销毁线程池会消耗很多资源. 合理利用线程池能够带来三个好处. 1降低资源消耗.通过重复利用已创建的线程降低线程创建和销毁造 ...