Python|每日一练|树:翻转二叉树

翻转二叉树

翻转一棵二叉树。

Tips:翻转二叉树

是指将二叉树中每个节点的左右子树交换位置后得到的新二叉树。具体操作如下:

  1. 递归地遍历整个二叉树,对于每个节点,将其左右子树交换位置,同时更新节点的值。

  2. 如果当前节点为空,则直接返回。

  3. 如果当前节点不为空,则递归处理其左右子树。

  4. 最后返回根节点即可。

下面是实现翻转二叉树的 Python 代码:

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
        
def invertTree(root):
    if not root:
        return None
    root.left, root.right = root.right, root.left
    invertTree(root.left)
    invertTree(root.right)
    return root

示例:

输入:

     4

/   \

2     7

/ \   / \

1   3 6   9

输出:

     4

/   \

7     2

/ \   / \

9   6 3   1

备注:
这个问题是受到 Max Howell (https://twitter.com/mxcl) 原问题(https://twitter.com/mxcl/status/608682016205344768) 启发的

Max Howell 是Mac OS X/iOS业界知名的软件工程师,也是Homebrew的原作者。 他在Google面试时,被HR让现场翻转二叉树,引发了程序员的吐槽和关注。

谷歌:我们90%的工程师使用您编写的软件(Homebrew),但是您却无法在面试时在白板上写出翻转二叉树这道题,这太糟糕了。

Homebrew是一款Mac OS平台下的软件包管理工具,拥有安装、卸载、更新、查看、搜索等很多实用的功能。简单的一条指令,就可以实现包管理,而不需要手动下载、解压、配置等繁琐步骤。

正确解答:

class TreeNode:

    def __init__(self, x):

        self.val = x

        self.left = None

        self.right = None


class Solution(object):

    def invertTree(self, root):

        """

      :type root: TreeNode

      :rtype: TreeNode

      """



        if not root:

            return None

        root.left, root.right = root.right, root.left



        self.invertTree(root.left)

        self.invertTree(root.right)



        return root

翻转二叉树的C语言实现

翻转二叉树的C语言思路如下:

  1. 首先,我们需要定义一个二叉树节点的结构体,包含节点值和左右子节点指针。

  2. 然后,我们编写一个递归函数,用于翻转二叉树。该函数的输入参数为当前节点指针,输出参数为翻转后的二叉树根节点指针。

  3. 在递归函数中,我们首先判断当前节点是否为空,如果为空则直接返回。

  4. 如果当前节点不为空,则交换当前节点的左右子节点指针,然后递归处理左右子树。

  5. 最后,我们在主函数中创建一个二叉树,并调用递归函数进行翻转操作。

  6. 翻转完成后,我们可以输出翻转后的二叉树,以验证翻转操作的正确性。

参考代码如下:

#include <stdio.h>
#include <stdlib.h>

// 定义二叉树节点结构体
typedef struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
} TreeNode;

// 递归函数,用于翻转二叉树
void invertTree(TreeNode* root) {
    if (root == NULL) {
        return;
    }
    // 交换左右子树
    TreeNode* temp = root->left;
    root->left = root->right;
    root->right = temp;
    // 递归翻转左右子树
    invertTree(root->left);
    invertTree(root->right);
}

// 测试代码
int main() {
    // 创建一棵二叉树
    TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode));
    root->val = 1;
    root->left = (TreeNode*)malloc(sizeof(TreeNode));
    root->left->val = 2;
    root->left->left = NULL;
    root->left->right = NULL;
    root->right = (TreeNode*)malloc(sizeof(TreeNode));
    root->right->val = 3;
    root->right->left = NULL;
    root->right->right = NULL;

    // 翻转二叉树
    invertTree(root);

    // 输出翻转后的二叉树(仅供参考)
    printf("inverted tree: ");
    printf("%d ", root->val);
    if (root->left != NULL) {
        printf("%d ", root->left->val);
        if (root->left->left != NULL) {
            printf("%d ", root->left->left->val);
        } else {
            printf("NULL ");
        }
        if (root->left->right != NULL) {
            printf("%d ", root->left->right->val);
        } else {
            printf("NULL ");
        }
    } else {
        printf("NULL ");
    }
    if (root->right != NULL) {
        printf("%d ", root->right->val);
        if (root->right->left != NULL) {
            printf("%d ", root->right->left->val);
        } else {
            printf("NULL ");
        }
        if (root->right->right != NULL) {
            printf("%d ", root->right->right->val);
        } else {
            printf("NULL ");
        }
    } else {
        printf("NULL ");
    }
    printf("
");

    return 0;
}
//(内容由讯飞星火AI生成)

翻转二叉树的java语言实现思路

  1. 首先,我们需要定义一个二叉树节点类,包含左右子节点和节点值。
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
        val = x;
    }
}

2、然后,我们需要实现一个递归函数,用于翻转二叉树。在这个函数中,我们需要先判断当前节点是否为空,如果为空则直接返回。接下来,我们需要交换当前节点的左右子节点,然后递归地翻转左子树和右子树。

public class Solution {
    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }

        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;

        invertTree(root.left);
        invertTree(root.right);

        return root;
    }
}

这样,我们就实现了翻转二叉树的功能。

(内容由讯飞星火AI生成)。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

打酱油的工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值