大牛没有能做出来的题,我们要好好做一做

Invert a binary tree.

  1. 4
  2. / \
  3. 2 7
  4. / \ / \
  5. 1 3 6 9

to

  1. 4
  2. / \
  3. 7 2
  4. / \ / \
  5. 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.

递归解决方案:

  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. {
  14. if(root ==NULL) return root;
  15. TreeNode* node = invertTree(root->left);
  16. root->left = invertTree(root->right);
  17. root->right = node;
  18. return root;
  19. }
  20. };

非递归解决方案:

  1. TreeNode* invertTree(TreeNode* root)
  2. {
  3. if(root == NULL)return NULL;
  4. vector<TreeNode*> stack;
  5. stack.push_back(root);
  6. while(!stack.empty())
  7. {
  8. TreeNode* node = stack.back();// or stack.top()
  9. stack.pop_back();
  10. swap(node->left,node->right);
  11. if(node->left)stack.push_back(node->left);
  12. if(node->right)stack.push_back(node->right);
  13. }
  14. return root;
  15. }

python:

  1. def invertTree(self, root):
  2. if root:
  3. root.left, root.right = self.invertTree(root.right), self.invertTree(root.left)
  4. return root
  5.  
  6. Maybe make it four lines for better readability:
  7.  
  8. def invertTree(self, root):
  9. if root:
  10. invert = self.invertTree
  11. root.left, root.right = invert(root.right), invert(root.left)
  12. return root
  13.  
  14. --------------------------------------------------------------------------------
  15.  
  16. And an iterative version using my own stack:
  17.  
  18. def invertTree(self, root):
  19. stack = [root]
  20. while stack:
  21. node = stack.pop()
  22. if node:
  23. node.left, node.right = node.right, node.left
  24. stack += node.left, node.right
  25. return root
  1. def invertTree(self, root):
  2. if root is None:
  3. return None
  4. root.left, root.right = self.invertTree(root.right), self.invertTree(root.left)
  5. return root

python非递归解决方案:

  1. DFS version:
  2.  
  3. def invertTree(self, root):
  4. if (root):
  5. self.invertTree(root.left)
  6. self.invertTree(root.right)
  7. root.left, root.right = root.right, root.left
  8. return root
  9.  
  10. BFS version:
  11.  
  12. def bfs_invertTree(self, root):
  13. queue = collections.deque()
  14. if (root):
  15. queue.append(root)
  16.  
  17. while(queue):
  18. node = queue.popleft()
  19. if (node.left):
  20. queue.append(node.left)
  21. if (node.right):
  22. queue.append(node.right)
  23. node.left, node.right = node.right, node.left
  24.  
  25. return root

leetcode 226 Invert Binary Tree 翻转二叉树的更多相关文章

  1. 【LeetCode】226. Invert Binary Tree 翻转二叉树(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址: https://lee ...

  2. 226. Invert Binary Tree 翻转二叉树

    [抄题]: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 [暴力解法]: 时间分析: 空间分 ...

  3. [LintCode] Invert Binary Tree 翻转二叉树

    Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...

  4. Leetcode 226 Invert Binary Tree python

    题目: Invert a binary tree. 翻转二叉树. 递归,每次对节点的左右节点调用invertTree函数,直到叶节点. python中也没有swap函数,当然你可以写一个,不过pyth ...

  5. [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 wa ...

  6. LeetCode 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 ...

  7. LeetCode 226 Invert Binary Tree(转换二叉树)

    翻译 将下图中上面的二叉树转换为以下的形式.详细为每一个左孩子节点和右孩子节点互换位置. 原文 如上图 分析 每次关于树的题目出错都在于边界条件上--所以这次细致多想了一遍: void swapNod ...

  8. Leetcode 226 Invert Binary Tree 二叉树

    交换左右叶子节点 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...

  9. Leetcode 226. Invert Binary Tree(easy)

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...

随机推荐

  1. Linux 查看CPU温度

    安装 lm-sensors sudo apt-get install lm-sensors # 安装yes | sudo sensors-detect # 侦测所有感测器 sensors # 查看温度 ...

  2. Android开发学习之路--Java和Js互相调用

      随着前端的火热,以前开发的快速,越来越多的native app在其中融合了h5,就拿淘宝就是很多的h5组成的,一旦出现什么节日,他都可以不用通过更新app来实现界面的改变,而且android和io ...

  3. 使用GDAL进行RPC坐标转换

    使用GDAL进行RPC坐标转换 对于高分辨率遥感卫星数据而言,目前几乎都提供了有理函数模型(RFM)来进行图像校正(SPOT系列提供了有理函数模型之外还提供了严格轨道模型).对遥感影像进行校正目前最常 ...

  4. 初识在Spring Boot中使用JPA

    前面关于Spring Boot的文章已经介绍了很多了,但是一直都没有涉及到数据库的操作问题,数据库操作当然也是我们在开发中无法回避的问题,那么今天我们就来看看Spring Boot给我们提供了哪些疯狂 ...

  5. 给定一数组,输出满足2a=b(a,b代表数组中的数)的数对,要求时间复杂度尽量低。

    //时间复杂度O(n),空间复杂度O(n) void findSequence(int* arr, int len) { int* hashtable = new int[RANGE]; memset ...

  6. 【Java二十周年】Delphi转行java的一些小感触

    本文纯属一届小码农对java使用过程的体验感触 目录: 初遇java编程语言 与java的擦肩 深入java 跨平台性 开源支持 web的支撑 初遇java编程语言 刚上大学的时候,完全是个电脑盲.刚 ...

  7. hive 压缩全解读(hive表存储格式以及外部表直接加载压缩格式数据);HADOOP存储数据压缩方案对比(LZO,gz,ORC)

    数据做压缩和解压缩会增加CPU的开销,但可以最大程度的减少文件所需的磁盘空间和网络I/O的开销,所以最好对那些I/O密集型的作业使用数据压缩,cpu密集型,使用压缩反而会降低性能. 而hive中间结果 ...

  8. CoreAnimation中layer动画闪烁的原因及解决

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 网上有一段Core Animation层动画的例子,是将vie ...

  9. [linux RedHat]windows下使用putty远程连接linux 下载JDK和tomcat

    本文地址:http://blog.csdn.net/sushengmiyan/article/details/43154543 本文作者:sushengmiyan ------------------ ...

  10. Struts 1 之配置文件

    web.xml中配置Struts的入口Servlet--ActionServlet,ActionServlet不负责任何的业务处理,它只是查找Action名单,找到path属性与URL属性一致的Act ...