Description
Invert a binary tree.
4
/ \
2 7
/ \ / \
1 3 6 9
to
4
/ \
7 2
/ \ / \
9 6 3 1
Trivia:
This problem was inspired by this original tweet by Max Howell:
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.
My close solution
class Solution {
public:
TreeNode *invertTree(TreeNode *root) {
if(root==NULL) return NULL;
root->left = invertTree(root->right);
root->right = invertTree(root->left);
return root;
}
};
上述代码基本思路正确, 但是写法上犯了swap错误写法的低级错误.
Discuss
- C++ 递归
TreeNode* invertTree(TreeNode* root) {
if (root) {
invertTree(root->left);
invertTree(root->right);
std::swap(root->left, root->right);
}
return root;
}
- Java 递归
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return null;
}
TreeNode right = invertTree(root.right);
TreeNode left = invertTree(root.left);
root.left = right;
root.right = left;
return root;
}
上述两种写法基本思路是一致的, 尤其是java版本, 跟我的solution只差一个显示的swap正确写法.
- C++ stack dfs
TreeNode* invertTree(TreeNode* root) {
std::stack<TreeNode*> stk;
stk.push(root);
while (!stk.empty()) {
TreeNode* p = stk.top();
stk.pop();
if (p) {
stk.push(p->left);
stk.push(p->right);
std::swap(p->left, p->right);
}
}
return root;
}
递归改迭代
二叉树先序遍历递归算法伪码
void PreorderRecursive(Bitree root){
if (root) {
/// 先visit
visit(root);
// 再压入子树
PreorderRecursive(root->lchild);
PreorderRecursive(root->rchild);
}
}
二叉树先序遍历非递归伪码
void PreorderNonRecursive(Bitree root){
stack stk;
stk.push(root);
while(!stk.empty()){
p = stk.top();
/// 先对p进行处理!!!!!
visit(p);
stk.pop();
/// 再压入子树
/// 注意这里先压入的right, 这样子才是和上面等价的
if(p.rchild) stk.push(stk.rchild);
if(p.lchild) stk.push(stk.lchild);
}
}
自己修改递归为stack形式, 与参考略有不同
/// 修改递归为stack的迭代形式
class Solution {
public:
TreeNode *invertTree(TreeNode *root) {
if(!root) return root;
stack<TreeNode *> s;
TreeNode *cur;
s.push(root);
while (!s.empty()) {
cur = s.top();
s.pop();
/// 1. 注意这里先处理cur, 即visit(cur)的所有应该做的处理
TreeNode *left = cur->left;
TreeNode *right = cur->right;
cur->left = right;
cur->right = left;
/// 2. cur遍历过了, 压入两个子树
if (cur->right) s.push(cur->right);
if (cur->left) s.push(cur->left);
}
return root;
}
};