本文主要是介绍Leetcode 107 Binary Tree Level Order Traversal II(水),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目连接:Leetcode 107 Binary Tree Level Order Traversal II
解题思路:参考Leetcode 102 和103,只要最后将102中的答案翻转一下即可。
/*** 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>> levelOrderBottom(TreeNode* root) {vector<vector<int>> ans;if (root == NULL) return ans;queue<TreeNode*> que;que.push(root);while (!que.empty()) {vector<int> cur;int n = que.size();while (n--) {TreeNode* t = que.front();que.pop();cur.push_back(t->val);if (t->left != NULL) que.push(t->left);if (t->right != NULL) que.push(t->right);}ans.push_back(cur);}reverse(ans.begin(), ans.end());return ans;}
};
这篇关于Leetcode 107 Binary Tree Level Order Traversal II(水)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!