翻转二叉树
来源:互联网 发布:呼市吉林大学网络教育 编辑:程序博客网 时间:2024/06/05 15:06
翻转二叉树
题目描述:
翻转一棵二叉树。
样例
1 1 / \ / \2 3 => 3 2 / \ 4 4
解题思路:
利用递归算法,交换左子树和右子树。
实现代码:
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;
swap(root->left,root->right);
invertBinaryTree(root->left);
invertBinaryTree(root->right);
}
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;
swap(root->left,root->right);
invertBinaryTree(root->left);
invertBinaryTree(root->right);
}
};
A题感悟:
大一学的STL现在几乎忘得差不多了,一开始根本没想到swap()这个函数,我是用了三四行代码把左子树和右子树的交换了的,可是提交之后数据不完全对,一直改不过来,就问了一下大神,才知道用这个函数如此简单。
0 0
- C++二叉树翻转
- 翻转二叉树
- 翻转二叉树
- 题目:翻转二叉树
- LintCode 翻转二叉树
- LinkCode-翻转二叉树
- 翻转二叉树
- 翻转二叉树
- 翻转二叉树
- LintCode_175_翻转二叉树
- 二叉树翻转
- lintcode ----翻转二叉树
- lintcode,翻转二叉树
- 翻转二叉树
- 翻转二叉树
- 翻转二叉树
- 翻转二叉树
- LintCode-翻转二叉树
- 访问web-inf下的jsp
- ARP欺骗
- Jetty 的工作原理以及与 Tomcat 的比较(转载)
- Problem G: 来开个书店吧
- 2017年院赛B题 N个数求和
- 翻转二叉树
- gg
- 十七、Mysql子查询
- try--catch语句输出顺序
- 基于Python的PIL库学习(一)
- python for data analysis
- 递推递归-J
- 浅谈数据挖掘中的关联规则挖掘
- 树莓派上Ubuntu14.04 mate设置