leetcode 226. Invert Binary Tree | DFS 递归转迭代

本文介绍了二叉树翻转算法的实现方法,包括递归和迭代两种方式,并提供了C++和Java的代码示例。文章还讨论了递归算法转换为非递归算法的技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
    }
};

Reference

-leetcode 226

-递归算法转换为非递归算法的技巧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值