226. Invert Binary Tree(Tree)
阿新 • • 發佈:2019-01-01
https://leetcode.com/problems/invert-binary-tree/description/
題目:將二叉樹進行翻轉
思路:使用BFS進行遍歷,然後交換每個結點的左右子節點。
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(!root) return root;
TreeNode *q[10000];
int l=0,r=1;
q[l]=root;
while (l<r){
if(q[l]){
TreeNode *temp = q[l]->left;
q[l]->left = q[l]->right;
q[l]->right=temp;
q[r]=q[l]->left;
r++;
q[r]=q[l]->right;
r++;
}
l++;
}
return root;
}
};