【easy】226. Invert Binary Tree 反转二叉树
- /**
- * 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 NULL;
- }
- TreeNode *tempNode = root->left;
- root->left = root->right;
- root->right = tempNode;
- invertTree(root->left);
- invertTree(root->right);
- 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;
- }
【easy】226. Invert Binary Tree 反转二叉树的更多相关文章
- 226. Invert Binary Tree 翻转二叉树
[抄题]: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 [暴力解法]: 时间分析: 空间分 ...
- 【LeetCode】226. Invert Binary Tree 翻转二叉树(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址: https://lee ...
- leetcode 226 Invert Binary Tree 翻转二叉树
大牛没有能做出来的题,我们要好好做一做 Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Tri ...
- 第27题:Leetcode226: Invert Binary Tree反转二叉树
翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 思路 如果根节点存在,就交换两个子树的根节点,用递归 ...
- LeetCode Invert Binary Tree 反转二叉树
思路:递归解决,在返回root前保证该点的两个孩子已经互换了.注意可能给一个Null. C++ /** * Definition for a binary tree node. * struct Tr ...
- Python解Leetcode: 226. Invert Binary Tree
leetcode 226. Invert Binary Tree 倒置二叉树 思路:分别倒置左边和右边的结点,然后把根结点的左右指针分别指向右左倒置后返回的根结点. # Definition for ...
- <LeetCode OJ> 226. Invert Binary Tree
226. Invert Binary Tree Total Accepted: 57653 Total Submissions: 136144 Difficulty: Easy Invert a bi ...
- LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree
258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...
- C#版 - 226. Invert Binary Tree(剑指offer 面试题19) - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - 2 ...
随机推荐
- xgboost 参数调优指南
一.XGBoost的优势 XGBoost算法可以给预测模型带来能力的提升.当我对它的表现有更多了解的时候,当我对它的高准确率背后的原理有更多了解的时候,我发现它具有很多优势: 1 正则化 标准GBDT ...
- 本文详解5G是个什么鬼,程序员都准备好了吗?
无线移动通讯发展历史 最近5G的概念炒的如火如荼,为此,华为和高通还干了一仗.这篇文章从技术层面给大家分析,什么是5G,它和4G比,高级在哪里? 我们来看看移动互联网的技术发展: 然后我们在来看看他们 ...
- 关于表单元素的name及HTML中的id
这种在上高级WEB课时,老师为表单元素赋了name值,之后直接在JS中使用该值而不需要使用document.get...来获取了,例: <!DOCTYPE html> <html&g ...
- 判断语句之单if
什么是判断语句? 给定一个判断条件,并在程序执行过程中判断该条件是否成立,根据判断结果执行不同的操作,从而改变代码的执行顺序,实现更多的功能,这就是判断语句. 判断语句if if语句第一种格式:if ...
- python2.7.14安装部署(Linux)
+++++++++++++++++++++++++++++++++++++++++++标题:python2.7.14安装部署(Linux)时间:2019年2月23日内容:Linux下python环境部 ...
- Android艺术——探究Handler运行机制
我们从开发的角度来说,Handler是Android 的消息机制的上层接口.说到Handler,大家都会说:哦,Handler这个我知道干什么的,更新UI.没错,Handler的确是用于更新UI的,具 ...
- Lodop简答问答大全
其他相关简短问答:Lodop简短问答客户反馈篇 及排查步骤 及注册相关,Lodop某个电脑打印内容大小有问题,LODOP超文本简短问答和相关内容,LODOP.C-Lodop简短排查语句.Lodop.c ...
- SPP-net原理解读
转载自:目标检测:SPP-net 地址https://blog.csdn.net/tinyzhao/article/details/53717136 上文说到R-CNN的最大瓶颈是2k个候选区域都要经 ...
- [https]公司导入自签名证书实现https监控
https://www.v2ex.com/t/143012
- Time travel HDU - 4418(高斯消元)
Agent K is one of the greatest agents in a secret organization called Men in Black. Once he needs to ...