【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->right;
            root->right = node;
            root->left = invertTree(root->left);
            root->right = invertTree(root->right);
        }
        return root;
    }
}

思路二:迭代

类似深度优先。

代码

时间复杂度:O(n)
空间复杂度:O(n)

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if (!root) {
            return root;
        }
        queue<TreeNode*> q;
        q.push(root);
        while (!q.empty()) {
            TreeNode *node = q.front();
            TreeNode *tmp = node->left;
            node->left = node->right;
            node->right = tmp;
            q.pop();
            if (node->left) {
                q.push(node->left);
            }
            if (node->right) {
                q.push(node->right);
            }
        }
        return root;
    }
};

原文地址:https://www.cnblogs.com/galaxy-hao/p/12354929.html

时间: 2024-11-05 22:01:10

【LeetCode】226. 翻转二叉树的相关文章

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

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. 翻转二叉树

翻转一棵二叉树. 示例: 思想 递归 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

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): ""

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-&

[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

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 226 Invert Binary Tree python

题目: Invert a binary tree. 翻转二叉树. 递归,每次对节点的左右节点调用invertTree函数,直到叶节点. python中也没有swap函数,当然你可以写一个,不过python中可以通过:a, b = b, a交换两个变量的值 1 class Solution(object): 2 def invertTree(self, root): 3 if root == None: return root 4 root.left, root.right = self.inve