leetcode:Invert Binary Tree
Invert a binary tree.
4
/ \
2 7
/ \ / \
1 3 6 9
to
4
/ \
7 2
/ \ / \
9 6 3 1
即反转二叉树,代码如下:
/**
* 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 { //在每一层将节点的左孩子和右孩子分别交换即可,直到节点没有孩子(递归recursion)
public:
TreeNode* invertTree(TreeNode* root) {
TreeNode *temp;
if(root)
{
temp = root->right;
root->right = invertTree(root->left);
root->left = invertTree(temp);
}
return root;
}
};
或者:
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (root) {
root->left = invertTree(root->left), root->right = invertTree(root->right);
std::swap(root->left, root->right);
}
return root;
}
};
其他解法:
1、C++ using queue, 0ms
非递归版本,做广度优先处理,使用一个队列来保存当前水平未处理的非空节点。每次,切换前节点的左,右指针,将其弹出,并添加它的非空孩子到队列中。
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if (root==nullptr) return nullptr;
queue<TreeNode*> st;
st.push(root);
while(!st.empty()){
TreeNode* n=st.front();
st.pop();
if (n->left!=NULL)
st.push(n->left);
if (n->right!=NULL)
st.push(n->right);
swap(n->left,n->right);
}
return root;
}
};
2、先前序遍历这棵树的每个结点,如果遍历到的结点有子节点,就交换他的两个子节点,当交换完所有非叶子节点的左右子节点之后,就得到了该二叉树的镜像。
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(root==null){
return null;
}
if(root->left==null&&root->right==null){
return root;
}
TreeNode *temp = root->left;
root->left = root->right;
root->right = temp;
if(root->left!=null){
invertTree(root->left);
}
if(root->right!=null){
invertTree(root->right);
}
return root;
}
};
显示出错:‘null’ was not declared in this scope
将null改为nullptr就 Accepted了:
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(root==nullptr){
return nullptr;
}
if(root->left==nullptr&&root->right==nullptr){
return root;
}
TreeNode *temp = root->left;
root->left = root->right;
root->right = temp;
if(root->left!=nullptr){
invertTree(root->left);
}
if(root->right!=nullptr){
invertTree(root->right);
}
return root;
}
};
【C++11】nullptr关键字:从1972年C语言刚刚诞生以来,常数0就扮演着整数(int)0和空指针( null pointer )两种角色。为了避免理解上的二义性,C语言通常使用NULL宏来表示空指针,NULL宏通常被定义为(void *)0或0, 而C++仅仅采用0来表示空指针,这样存在一个问题:比如对于重载函数 fun(char *) 和 fun(int) 的调用来说,若直接用NULL作为参数调用fun(NULL),我们可能认为NULL作为空指针的表示,应该调用 fun(char *) 函数,然而NULL实际上的值为0,就会调用 fun(int) 。 nullptr 关键字正是为了解决这一问题而产生的。
nullptr 能够转换成任何指针类型(包括成员函数指针和成员变量指针)和bool类型(这是为了兼容普通指针都能使用 if(ptr) 判断是否为空指针的形式),但是不能被转换为整数0。
(更多了解可参考:http://blog.csdn.net/huang_xw/article/details/8764346)
leetcode:Invert Binary Tree的更多相关文章
- LeetCode OJ: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 (反转二叉树)
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...
- Java for 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 wa ...
- (easy)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 ...
- Java [Leetcode 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
题目: Invert a binary tree. 翻转二叉树. 递归,每次对节点的左右节点调用invertTree函数,直到叶节点. python中也没有swap函数,当然你可以写一个,不过pyth ...
- 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 ...
- 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 ...
随机推荐
- Visual Studio快捷键设置
1.查看当前快捷键:环境-键盘-按快捷键2.文本编辑器-C#-显示-行号3.文本编辑器-C#-制表符-插入空格4.文本编辑器-所有语言-没有选定内容时对空行应用剪切或复制命令5.Ctrl+Shift- ...
- Springmvc jar包介绍
spring.jar是包含有完整发布的单个jar 包,spring.jar中包含除了spring-mock.jar里所包含的内容外其它所有jar包的内容,因为只有在开发环境下才会用到 spring-m ...
- [百度空间] --whole-archive & --no-whole-archive
What is it? backgorund: an archive file (.a) is similar as .lib compared to Winodws. it simply conta ...
- DreamFactory service platform 将DB发布成restful service
PPT:http://www.slideshare.net/DreamFactorySoftware/angularjs-and-rest-made-simple blog:http://blog.d ...
- 【算法】E.W.Dijkstra算术表达式求值
算术表达式求值 我们要学习的一个栈的用例同时也是展示泛型的应用的一个经典例子,就是用来计算算术表达式的值,例如 ( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) ) 如果将4乘以5,把3 ...
- code::blocks 初使用遇到的问题记录
/* 做本程序遇到的问题:由于使用的是CODE::BLOCKS 开发环境,刚开始使用code::blocks是,什么都 没有设置,居然输入的中文字符串,保存项目后,再次打开,code::blocks不 ...
- React Native 简介:用 JavaScript 搭建 iOS 应用(2)
[编者按]本篇文章的作者是 Joyce Echessa--渥合数位服务创办人,毕业于台湾大学,近年来专注于协助客户进行 App 软体以及网站开发.本篇文章中,作者介绍通过 React Native 框 ...
- 这个东西,写C++插件的可以用到。 RapidJSON —— C++ 快速 JSON 解析器和生成器
点这里 原文: RapidJSON —— C++ 快速 JSON 解析器和生成器 时间 2015-04-05 07:33:33 开源中国新闻原文 http://www.oschina.net/p/ ...
- 借助flexpaper实现word在线预览和打印
为了实现word能够在web上尽量以原始的排版样式展现出来,选择基于activex控件的方式太过于依赖某种浏览器,并且存在可能需要花费金钱购买相应的控件产品:于是借助flexpaper这种flash展 ...
- DOS永久设置系统环境变量-WMIC
wmic Windows Management Instrumentation Command-line(Windows管理规范命令行) WMIC扩展WMI(Windows Management In ...