leetcode 226. Invert Binary Tree

Invert a binary tree.

     4
   /   \
  2     7
 / \   / \
1   3 6   9
to
     4
   /   \
  7     2
 / \   / \
9   6 3   1


class Solution {
public:
	TreeNode* invertTree(TreeNode* root) {

		if(root==NULL)
        return NULL;
        vector<TreeNode*>que;
        que.push_back(root);
        while(!que.empty())
        {
            vector<TreeNode*>newque;
        for(int i=0;i<que.size();i++)
        {
            if(que[i]->left!=NULL)
            newque.push_back(que[i]->left);
            if(que[i]->right!=NULL)
            newque.push_back(que[i]->right);
        }
		for (int i = 0; i < que.size(); i++)
		{
			TreeNode*l = que[i]->left;
			que[i]->left = que[i]->right;
			que[i]->right = l;
		}
        que=newque;
        }
        return root;
	}
};

accept


你可能感兴趣的:(LeetCode)