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 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.
递归解决方案:
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * TreeNode *left;
- * TreeNode *right;
- * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
- * };
- */
- class Solution {
- public:
- TreeNode* invertTree(TreeNode* root)
- {
- if(root ==NULL) return root;
- TreeNode* node = invertTree(root->left);
- root->left = invertTree(root->right);
- root->right = node;
- return root;
- }
- };
非递归解决方案:
- TreeNode* invertTree(TreeNode* root)
- {
- if(root == NULL)return NULL;
- vector<TreeNode*> stack;
- stack.push_back(root);
- while(!stack.empty())
- {
- TreeNode* node = stack.back();// or stack.top()
- stack.pop_back();
- swap(node->left,node->right);
- if(node->left)stack.push_back(node->left);
- if(node->right)stack.push_back(node->right);
- }
- return root;
- }
python:
- def invertTree(self, root):
- if root:
- root.left, root.right = self.invertTree(root.right), self.invertTree(root.left)
- return root
- Maybe make it four lines for better readability:
- def invertTree(self, root):
- if root:
- invert = self.invertTree
- root.left, root.right = invert(root.right), invert(root.left)
- return root
- --------------------------------------------------------------------------------
- And an iterative version using my own stack:
- def invertTree(self, root):
- stack = [root]
- while stack:
- node = stack.pop()
- if node:
- node.left, node.right = node.right, node.left
- stack += node.left, node.right
- return root
- def invertTree(self, root):
- if root is None:
- return None
- root.left, root.right = self.invertTree(root.right), self.invertTree(root.left)
- return root
python非递归解决方案:
- DFS version:
- def invertTree(self, root):
- if (root):
- self.invertTree(root.left)
- self.invertTree(root.right)
- root.left, root.right = root.right, root.left
- return root
- BFS version:
- def bfs_invertTree(self, root):
- queue = collections.deque()
- if (root):
- queue.append(root)
- while(queue):
- node = queue.popleft()
- if (node.left):
- queue.append(node.left)
- if (node.right):
- queue.append(node.right)
- node.left, node.right = node.right, node.left
- return root
leetcode 226 Invert Binary Tree 翻转二叉树的更多相关文章
- 【LeetCode】226. Invert Binary Tree 翻转二叉树(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址: https://lee ...
- 226. Invert Binary Tree 翻转二叉树
[抄题]: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 [暴力解法]: 时间分析: 空间分 ...
- [LintCode] Invert Binary Tree 翻转二叉树
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. ...
- Leetcode 226 Invert Binary Tree python
题目: Invert a binary tree. 翻转二叉树. 递归,每次对节点的左右节点调用invertTree函数,直到叶节点. python中也没有swap函数,当然你可以写一个,不过pyth ...
- [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 ...
- 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 ...
- LeetCode 226 Invert Binary Tree(转换二叉树)
翻译 将下图中上面的二叉树转换为以下的形式.详细为每一个左孩子节点和右孩子节点互换位置. 原文 如上图 分析 每次关于树的题目出错都在于边界条件上--所以这次细致多想了一遍: void swapNod ...
- Leetcode 226 Invert Binary Tree 二叉树
交换左右叶子节点 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...
- 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 ...
随机推荐
- Linux 查看CPU温度
安装 lm-sensors sudo apt-get install lm-sensors # 安装yes | sudo sensors-detect # 侦测所有感测器 sensors # 查看温度 ...
- Android开发学习之路--Java和Js互相调用
随着前端的火热,以前开发的快速,越来越多的native app在其中融合了h5,就拿淘宝就是很多的h5组成的,一旦出现什么节日,他都可以不用通过更新app来实现界面的改变,而且android和io ...
- 使用GDAL进行RPC坐标转换
使用GDAL进行RPC坐标转换 对于高分辨率遥感卫星数据而言,目前几乎都提供了有理函数模型(RFM)来进行图像校正(SPOT系列提供了有理函数模型之外还提供了严格轨道模型).对遥感影像进行校正目前最常 ...
- 初识在Spring Boot中使用JPA
前面关于Spring Boot的文章已经介绍了很多了,但是一直都没有涉及到数据库的操作问题,数据库操作当然也是我们在开发中无法回避的问题,那么今天我们就来看看Spring Boot给我们提供了哪些疯狂 ...
- 给定一数组,输出满足2a=b(a,b代表数组中的数)的数对,要求时间复杂度尽量低。
//时间复杂度O(n),空间复杂度O(n) void findSequence(int* arr, int len) { int* hashtable = new int[RANGE]; memset ...
- 【Java二十周年】Delphi转行java的一些小感触
本文纯属一届小码农对java使用过程的体验感触 目录: 初遇java编程语言 与java的擦肩 深入java 跨平台性 开源支持 web的支撑 初遇java编程语言 刚上大学的时候,完全是个电脑盲.刚 ...
- hive 压缩全解读(hive表存储格式以及外部表直接加载压缩格式数据);HADOOP存储数据压缩方案对比(LZO,gz,ORC)
数据做压缩和解压缩会增加CPU的开销,但可以最大程度的减少文件所需的磁盘空间和网络I/O的开销,所以最好对那些I/O密集型的作业使用数据压缩,cpu密集型,使用压缩反而会降低性能. 而hive中间结果 ...
- CoreAnimation中layer动画闪烁的原因及解决
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 网上有一段Core Animation层动画的例子,是将vie ...
- [linux RedHat]windows下使用putty远程连接linux 下载JDK和tomcat
本文地址:http://blog.csdn.net/sushengmiyan/article/details/43154543 本文作者:sushengmiyan ------------------ ...
- Struts 1 之配置文件
web.xml中配置Struts的入口Servlet--ActionServlet,ActionServlet不负责任何的业务处理,它只是查找Action名单,找到path属性与URL属性一致的Act ...