标签:tree btn log 维数 数组 node reverse example evel
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,null,null,15,7]
,
3 / 9 20 / 15 7
return its bottom-up level order traversal as:
[ [15,7], [9,20], [3] ]
class Solution { public: vector<vector<int>> levelOrderBottom(TreeNode* root) { vector<vector<int>> res; if (root == nullptr) return res; queue<TreeNode*> q; q.push(root); while (!q.empty()) { int n = q.size(); vector<int> cur; for (int i = 0; i != n; i++) { TreeNode* node = q.front(); q.pop(); cur.push_back(node->val); if (node->left != nullptr) q.push(node->left); if (node->right != nullptr) q.push(node->right); } res.push_back(cur); } reverse(res.begin(), res.end()); return res; } }; // 3 ms
[LeetCode] Binary Tree Level Order Traversal II
标签:tree btn log 维数 数组 node reverse example evel
原文地址:http://www.cnblogs.com/immjc/p/7225544.html