您的位置:首页 > 其它

lintcode翻转二叉树

2017-11-02 23:41 316 查看


翻转二叉树 

 描述
 笔记

 数据

 评测



Accepted

总耗时: 254 ms
100% 数据通过测试.
还没解决的相关题目
26
%389. 判断数独是否合法容易20
%248. 统计比给定整数小的数的个数中等19
%249. 统计前面比自己小的数的个数困难14
%131. 大楼轮廓超难26
%370. 将表达式转换为逆波兰表达式困难

太牛了,把AC的喜悦分享给你的朋友吧!

解析:这道题的树并不是二叉查找树,所以对每个结点的左右结点交换即可。

/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/

class Solution {
public:
/*
* @param root: a TreeNode, the root of the binary tree
* @return: nothing
*/
void invertBinaryTree(TreeNode * root) {
// write your code here
if(root==NULL)
return ;
invertBinaryTree(root->left);//后续遍历,先对左右结点操作
invertBinaryTree(root->right);
TreeNode *tmp;
tmp=root->left;
root->left=root->right;
root->right=tmp;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  二叉树 翻转