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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. (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 ...

  5. 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 解题思路: 我只想说递归大法好. ...

  6. Leetcode 226 Invert Binary Tree python

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

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

  8. 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 ...

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

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

随机推荐

  1. pragma伪指令

    pragma伪指令 通过pragma伪指令告诉编译器如何对待特定的函数.对象或代码段.TMS320C28x C/C++编译器支持如下形式的pragma伪指令: CODE_SECTION(func,“s ...

  2. 用include来处理模板的问题

    /** * 测试方法 */ protected function getHtml() { $tpl = $this->pageletDir.$this->plTemplate; $html ...

  3. themeforest 模板

    如果给个人或一个客户使用就购买Regular License 多个项目或多人就徐需要购买Extended License,然后看你买html模版还是wordpress模版了.html需要你自己会编程将 ...

  4. 还原TexturePacker plist 文件以及图片的方法 (切开各小图片)

    原地址:http://blog.csdn.net/linuxchen/article/details/16865645 Python 脚本:(来自网络) unpack_plist.py 命令行: py ...

  5. C# 序列化 Serialize 的应用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  6. 让IE支持max-width

    1:expression在FF下不支持 2:*html内的width不要带单位(px). 3:width:expression(eval(this.offsetWidth>1600?1600:t ...

  7. 由浅入深了解Thrift之微服务化应用架构

    为什么选择微服务 一般情况下,业务应用我们都会采用模块化的分层式架构,所有的业务逻辑代码最终会在一个代码库中并统一部署,我们称这种应用架构为单体应用. 单体应用的问题是,全部开发人员会共享一个代码库, ...

  8. Redis与Memcached的incr/decr差异对比

    目前广泛使用的分布式缓存Redis和Memcached均支持对整数型Value值的增减,对应到具体命令中就是incr和decr命令. incr/decr是原子性操作(memcached 1.2.4及以 ...

  9. HDU 1028 Ignatius and the Princess III (递归,dp)

    以下引用部分全都来自:http://blog.csdn.net/ice_crazy/article/details/7478802  Ice—Crazy的专栏 分析: HDU 1028 摘: 本题的意 ...

  10. C# TryXXXX模式

    public static int? TrayParse(string text) { int ret; if (int.TryParse(text,out ret)) { return ret; } ...