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 ...
随机推荐
- jQuery(Keep for myself)
jQuery API : http://www.w3cschool.cc/manual/jquery/ 1. jQuery是一个JavaScript函数库. jQuery是一个轻量级的"写的 ...
- WP手机升级WIN10被PIN码锁定
WP8.1手机升级WIN10后,需要输入PIN码(不知道啊),多次输入(1234,0000,8888 ...)后被锁定,无法使用手机(郁闷), 重启无数次,提示由于多次输入PIN码,手机无法使用(天啊 ...
- discuzx完全自定义设计模板门户首页,栏目,专题模板方法
第一种:门户首页模板(index.htm,保存于templatedefaultportal) <!--{subtemplate common/header}--> <style id ...
- Stacked injection--堆叠注入--堆查询注入
Stacked injection--堆叠注入--堆查询注入 原文地址;http://www.sqlinjection.net/stacked-queries/ 本篇属于集合原作者的思路和个人想法 ...
- 解决IE不支持position:fixed问题
#box { /* 非IE6浏览器使用固定元素 */ position:fixed; top:0; left:0; /* IE6改为绝对定位,并通过css表达式根据滚动位置更改top的值 */ _po ...
- NWR协议
NWR是一种在分布式存储系统中用于控制一致性级别的一种策略.在Amazon的Dynamo云存储系统中,就应用NWR来控制一致性. 让我们先来看看这三个字母的含义:N:在分布式存储系统中,有多少份备份数 ...
- 【译】Python中如何创建mock?
原文地址:http://engineroom.trackmaven.com/blog/making-a-mockery-of-python/ 今天我们来谈论下mock的使用.当然,请不要误会,这里的m ...
- POJ 2039
#include<iostream> #include<stdio.h> #include<string> #define MAXN 20 using namesp ...
- ext3grep
- Maven的安装
我对maven的了解,仅仅局限在百度百科. 由于近期公司需求,我找到了个maven教程:http://wentao365.iteye.com/blog/903396 安装maven其实很简单,就是在A ...