您的位置:首页 > 编程语言 > C语言/C++

175.翻转二叉树

2017-04-21 22:15 411 查看
题目:翻转一棵二叉树

样例:

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;
else
{
TreeNode *t=root->left;
root->left=root->right;
root->right=t;
invertBinaryTree(root->left);
invertBinaryTree(root->right);
}
}
};

感想:从根开始,左右子树调换,然后利用递归的思想,遍历左右子树,即可完成翻转。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++作业