Article Directory
1. Source title
Links: I. printed upside down binary tree
Source: LeetCode-- "prove safety -Offer" special
Links: II printed upside down binary II.
Source: LeetCode-- "prove safety -Offer" special
Link: . III printed upside down binary III
Source: LeetCode-- "prove safety -Offer" special
2. Description title
prompt:
节点总数 <= 1000
3. resolve title
One problem:. I print a binary tree from top to bottom
Method a: subsidiary queue traversal sequence + + General Method
Obviously binary tree traversal sequence, using an auxiliary queue. First, the first node into the team to start while
the cycle, then empty queue element, when the head of the queue element to be dequeued children around enqueued and the element push_back
to vector
the. When the queue is empty the loop terminates, i.e., the binary tree traversal of all nodes is completed.
See the code below:
// 执行用时 :4 ms, 在所有 C++ 提交中击败了87.08%的用户
// 内存消耗 :14.5 MB, 在所有 C++ 提交中击败了100.00%的用户
/**
* 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<int> levelOrder(TreeNode* root) {
vector<int> vt;
if (root == nullptr) return vt;
queue<TreeNode*> q;
TreeNode* cur;
q.push(root);
while (!q.empty()) {
for (int i = 0; i < q.size(); ++i) {
cur = q.front();
vt.push_back(cur->val);
q.pop();
if (cur->left) q.push(cur->left);
if (cur->right) q.push(cur->right);
}
}
return vt;
}
};
Question two: II printed upside down binary II.
Method a: subsidiary queue traversal sequence + + General Method
This problem and a problem are essentially binary tree traversal sequence, but has the form of changes in the output result, the problem needs to be returned vector<vector<int>>
, i.e. each layer into a binary data vector<int>
in which two-dimensional and then vector
added element go on the line. On the code a little change can be. Change position has been marked in code.
See the code below:
// 执行用时 :4 ms, 在所有 C++ 提交中击败了90.01%的用户
// 内存消耗 :15.1 MB, 在所有 C++ 提交中击败了100.00%的用户
/**
* 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>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
if (!root) return res;
vector<int> vt;
queue<TreeNode*> q;
TreeNode* cur;
int len = 1;
q.push(root);
while (!q.empty()) {
for (int i = 0; i < len; ++i) {
cur = q.front();
vt.push_back(cur -> val);
q.pop();
if (cur->left) q.push(cur -> left);
if (cur->right) q.push(cur->right);
}
res.push_back(vt); // 改动点
vt.clear(); // 改动点
len = q.size();
}
return res;
}
};
Question three:. III printed upside down binary III
Method a: subsidiary queue traversal sequence + + + layer parity flag General Method
Traversal sequence variants can be obviously found: binary tree root node is set, then the first layer, all sequences succession even layers are opposite to the original sequence. Here only to make a mark, the even layer vector
sequence do sequences reverse()
can.
See the code below:
// 执行用时 :4 ms, 在所有 C++ 提交中击败了86.05%的用户
// 内存消耗 :15.1 MB, 在所有 C++ 提交中击败了100.00%的用户
/**
* 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>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
if (!root) return res;
vector<int> vt;
queue<TreeNode*> q;
TreeNode* cur;
int len = 1, cnt = 1;
q.push(root);
while (!q.empty()) {
for (int i = 0; i < len; ++i) {
cur = q.front();
vt.push_back(cur -> val);
q.pop();
if (cur->left) q.push(cur -> left);
if (cur->right) q.push(cur->right);
}
if (cnt % 2 == 0) reverse(vt.begin(), vt.end()); // 注意点
res.push_back(vt);
vt.clear();
len = q.size();
++cnt;
}
return res;
}
};
Method two: secondary dual-stack layer preorder + + General Method
The method of the present problem encountered when a mass data, reverse()
slow operation and also lacks the technical content. This gives "to prove safety -Offer" in the 辅助双栈
solution of:
- Firstly, two auxiliary stack,
s1、s2
- Now binary tree root stack
s1
, we set the root node as the first layer s1
Root within the stack, data at this time needs to be a second layer (even-numbered layers) of the stacks2
, which stack way to left child and right child again , this will ensure that the same layer as the stack order is right to left. Until the stacks1
is emptys2
The stack order of the elements, when data needs to be a third layer (odd-numbered layers) of the stacks1
, which stack way to the right child of the left child again , this will ensure that the same layer as the stack order is from left to right. Until the stacks2
is empty- So back and forth, until the stack
s1
ands2
are empty, end
So reverse or to think 栈
of clever use ah.
See the code below:
// 执行用时 :16 ms, 在所有 C++ 提交中击败了6.88%的用户
// 内存消耗 :15.1 MB, 在所有 C++ 提交中击败了100.00%的用户
/**
* 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>> levelOrder(TreeNode* root) {
vector<vector<int>> res;
vector<int> vt;
if (root == nullptr) return res;
int len = 1;
stack<TreeNode*> s1, s2;
s2.push(root);
while (!s1.empty() or !s2.empty()) {
while (!s2.empty()) {
TreeNode* tmp = s2.top();
vt.push_back(tmp->val);
s2.pop();
if (tmp->left) s1.push(tmp->left);
if (tmp->right) s1.push(tmp->right);
}
if (vt.size()) res.push_back(vt);
vt.clear();
while (!s1.empty()) {
TreeNode* tmp = s1.top();
vt.push_back(tmp->val);
s1.pop();
if (tmp->right) s2.push(tmp->right);
if (tmp->left) s2.push(tmp->left);
}
if (vt.size()) res.push_back(vt);
vt.clear();
}
return res;
}
};