翻转二叉树
来源:互联网 发布:智能语音助手软件 编辑:程序博客网 时间:2024/05/16 10:15
问题描述:翻转一棵二叉树
样例:
1 1 / \ / \2 3 => 3 2 / \ 4 4
实现代码:
/**
* 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
TreeNode *temp;
if(root==NULL)
return;
else
{
temp=root->right;
root->right=root->left;
root->left=temp;
invertBinaryTree(root->left);
invertBinaryTree(root->right);
}
//swap(root->left,root->right);
//invertBinaryTree(root->left);
//invertBinaryTree(root->right); 大神给讲的swap()函数
}
};
做题感想:此题要注意定义的temp变量是局部变量。做完后看别的同学做的更简单,是什么swap()函数,好吧以前没好好学,不知道是啥。
- C++二叉树翻转
- 翻转二叉树
- 翻转二叉树
- 题目:翻转二叉树
- LintCode 翻转二叉树
- LinkCode-翻转二叉树
- 翻转二叉树
- 翻转二叉树
- 翻转二叉树
- LintCode_175_翻转二叉树
- 二叉树翻转
- lintcode ----翻转二叉树
- lintcode,翻转二叉树
- 翻转二叉树
- 翻转二叉树
- 翻转二叉树
- 翻转二叉树
- LintCode-翻转二叉树
- Pandas——ix vs loc vs iloc区别
- 170411 VJ题解(4)(ZOJ
- java day9
- Maximum Subarray
- 129. Sum Root to Leaf Numbers | 二叉树整数和
- 翻转二叉树
- Android APK打包。
- Laravel路由除了根目录全报404错误的解决办法
- 重新认识 Java 的 System.in
- 剑指offer——栈的压入、弹出序列____
- linux 装 svn
- Anroid软件自动扫描拍照识别名片SDK
- 第一篇文章
- 第一次写的代码,有点激动