-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathInvert-Binary-Tree.cpp
48 lines (48 loc) · 1.38 KB
/
Invert-Binary-Tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// C++ Solution 1:
/**
* 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:
TreeNode* invertTree(TreeNode* root) {
if (root==NULL)
return root;
TreeNode* temp = root->left;
root->left = invertTree(root->right);
root->right = invertTree(temp);
return root;
}
};
// C++ Solution 2:
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (root==NULL)
return root;
queue<TreeNode*> q;
//将二叉树中的节点逐层放入队列中,再迭代处理队列中的元素
q.push(root);
while (!q.empty())
{
//每次都从队列中拿一个节点,并交换这个节点的左右子树
TreeNode* cur = q.front();
q.pop();
TreeNode* temp = cur->left;
cur->left = cur->right;
cur->right = temp;
//如果当前节点的左子树不为空,则放入队列等待后续处理
if (cur->left!=NULL)
q.push(cur->left);
//如果当前节点的右子树不为空,则放入队列等待后续处理
if (cur->right!=NULL)
q.push(cur->right);
}
return root;
}
};