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  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     TreeNode* invertTree(TreeNode* root) {
13         if(root == NULL) return NULL;
14         TreeNode* rt = root->left;
15         root->left = invertTree(root->right);
16         root->right = invertTree(rt);
17         return root;
18     }
19 };

题思:是看题解之后敲出来的 需要会手写!!!

原文地址:https://www.cnblogs.com/jj81/p/9038678.html

时间: 2024-08-25 12:28:43

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

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

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

力扣(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-&

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

算法之:翻转二叉树

事情大概是说,Max Howell 去 Google 面试,面试官说:虽然在 Google 有 90% 的工程师用你写的 Homebrew,但是你居然不能在白板上写出翻转二叉树的代码,所以滚蛋吧. /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */