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 ...
随机推荐
- poi过滤操作后产生新的sheet
public Sheet filterSheet(Sheet sheetToFilter){ if(sheetToFilter == null){ System.out.println("s ...
- GS线程
void GameServer::ProcessThread() { try {//在ui线程里面搞个大try不是说try效率不好吗,难道只是为了出现错误发现在GS线程里面出现的吗 ProcessTh ...
- c++11 pod类型(了解)
啥是POD类型? POD全称Plain Old Data.通俗的讲,一个类或结构体通过二进制拷贝后还能保持其数据不变,那么它就是一个POD类型. 平凡的定义 .有平凡的构造函数 .有平凡的拷贝构造函数 ...
- windows下安装php5.2.*,php5.3.*,php5.4.*版本的memcache扩展(转)
拓展安装调试方法: 编写调试php文件 <?php memcache(); 通过命令行执行测试,因为php拓展安装成功与否与apache无关,所以没必要不断重启apache去看phpinfo ...
- javascript笔记 面向对象
Javascript是一种面向对象的弱语言,既然有面向对象,就有继承 继承: 1.call函数和apply函数:区别在于它们参数上的不同,固定参数的用call,可变参数的用apply.换句话说,就是a ...
- HDU2295 Radar (DLX)
下面的代码99%参考了这个网站http://www.cnblogs.com/183zyz/archive/2011/08/07/2130193.html 人生的第一道DLX肯定是需要作一些参考的啦. ...
- iOS多线程的初步研究(十)-- dispatch同步
GCD提供两种方式支持dispatch队列同步,即dispatch组和信号量. 一.dispatch组(dispatch group) 1. 创建dispatch组 dispatch_group_t ...
- INTERESTING AND OBSCURE INHERITANCE ISSUES WITH CPP
1. using 关键字 使用 using 关键字,可以将父类中被隐藏的函数暴露在子类中,但是需要注意的是,在相同情况下,子类函数的优先级更高. 2. 继承构造函数(C++11) 在c++11之前, ...
- Jmeter之Bean shell使用(二)
上一篇Jmeter之Bean shell使用(一)简单介绍了下Jmeter中的Bean shell,本文是对上文的一个补充,主要总结下常用的几种场景和方法,相信这些基本可以涵盖大部分的需求.本节内容如 ...
- Spring REST for DELETE Request Method Not Supoorted
http://stackoverflow.com/questions/22055251/sending-data-with-angularjs-http-delete-request I have a ...