标签:
Invert a binary tree.
4 / 2 7 / \ / 1 3 6 9to
4 / 7 2 / \ / 9 6 3 1Trivia:
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.
class Solution { public: TreeNode* invertTree(TreeNode* root) { if(root==NULL) return NULL; deque<TreeNode*> qu; qu.push_front(root); while(!qu.empty()) { TreeNode* node=qu.front(); TreeNode* left=node->left; node->left=node->right; node->right=left; qu.pop_front(); if(node->left) qu.push_front(node->left); if(node->right) qu.push_front(node->right); } return root; } };
class Solution { public: TreeNode* invertTree(TreeNode* root) { if(root==NULL) return NULL; TreeNode* left=root->left; TreeNode* right=root->right; root->left=invertTree(right); root->right=invertTree(left); return root; } };
标签:
原文地址:http://blog.csdn.net/sinat_24520925/article/details/46509115