226. 翻转二叉树python

翻转一棵二叉树。

示例:

输入:

     4
   /     2     7
 / \   / 1   3 6   9

输出:

4
   /     7     2
 / \   / 9   6 3   1
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def invertTree(self, root):
        """
        :type root: TreeNode
        :rtype: TreeNode
        """
        if root == None:
            return root
        temp = root.left
        root.left = root.right
        root.right = temp

        self.invertTree(root.left)
        self.invertTree(root.right)

        return root

  

原文地址:https://www.cnblogs.com/zyjdbk/p/9646353.html

时间: 2024-07-31 15:21:49

226. 翻转二叉树python的相关文章

226. 翻转二叉树

翻转一棵二叉树. 示例: 输入: 4 / 2 7 / \ / 1 3 6 9 输出: 4 / 7 2 / \ / 9 6 3 1 备注:这个问题是受到 Max Howell的 原问题 启发的 : 谷歌:我们90%的工程师使用您编写的软件(Homebrew),但是您却无法在面试时在白板上写出翻转二叉树这道题,这太糟糕了. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeN

226. 翻转二叉树 | Invert Binary Tree

Invert a binary tree. Example: Input: 4 / 2 7 / \ / 1 3 6 9 Output: 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

leetcode 226. 翻转二叉树

翻转一棵二叉树. 4 / 2 7 / \ / 1 3 6 9 转换为: 4 / 7 2 / \ / 9 6 3 1 注意点:小心不要把程序写成下面这样: 1 root->left = invertTree(root->right); 2 root->right = invert(root->left); 因为第一行的root->left指向的内容已近改变,要用一个变量来保存原来的root->left的值 1 /** 2 * Definition for a binary

力扣(LeetCode)226. 翻转二叉树

翻转一棵二叉树. 示例: 思想 递归 java版 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public TreeNode invertTree(TreeNode root) { if(root == nu

【LeetCode】226. 翻转二叉树

题目 翻转一棵二叉树. 示例: 输入: 4 / 2 7 / \ / 1 3 6 9 输出: 4 / 7 2 / \ / 9 6 3 1 本题同[剑指Offer]面试题27. 二叉树的镜像 思路一:递归 代码 时间复杂度:O(n) 空间复杂度:O(n) class Solution { public: TreeNode* invertTree(TreeNode* root) { if (root) { TreeNode *node = root->left; root->left = root-

leadcode的Hot100系列--226. 翻转二叉树

这玩意儿基本上还是遍历的那一套, 这里使用先序遍历的方式,直接对左右子树进行对调即可. (虽然看题目的时候,感觉都一样,但真正写出来之后,印象还是深刻了很多) struct TreeNode* invertTree(struct TreeNode* root){ struct TreeNode *pTemp = NULL; if (NULL == root) return NULL; pTemp = root->left; root->left = root->right; root-&

leecode刷题(24)-- 翻转二叉树

leecode刷题(24)-- 翻转二叉树 翻转二叉树 翻转一棵二叉树. 示例: 输入: 4 / 2 7 / \ / 1 3 6 9 输出: 4 / 7 2 / \ / 9 6 3 1 备注: 这个问题是受到 Max Howell的 原问题 启发的 : 谷歌:我们90%的工程师使用您编写的软件(Homebrew),但是您却无法在面试时在白板上写出翻转二叉树这道题,这太糟糕了. 思路 二叉树问题,我们首先要想到的使用递归的方式来解决,有了这个思路,处理这道问题就很简单了:先互换根节点的左右节点,然

226反转二叉树 Invert Binary Tree

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 wh

[LeetCode] Invert Binary Tree 翻转二叉树

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 w