175.翻转二叉树
2017-04-21 22:15
411 查看
题目:翻转一棵二叉树
样例:
代码:
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);
}
}
};
感想:从根开始,左右子树调换,然后利用递归的思想,遍历左右子树,即可完成翻转。
样例:
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);
}
}
};
感想:从根开始,左右子树调换,然后利用递归的思想,遍历左右子树,即可完成翻转。
相关文章推荐
- Lintcode 175. 翻转二叉树
- LintCode - 175.翻转二叉树
- 175. 翻转二叉树--java
- 175. 翻转二叉树
- [LintCode]175.翻转二叉树
- 【LintCode 简单】175. 翻转二叉树
- 翻转一棵二叉树
- leetCode之翻转二叉树
- 按行输出,之字形输出,翻转二叉树----层次遍历的应用
- 翻转二叉树(Invert Binary Tree)
- 翻转二叉树(leetcode-226)
- 翻转二叉树
- 翻转二叉树
- 翻转二叉树
- 翻转二叉树
- lintcode 容易题:Invert Binary Tree 翻转二叉树
- PAT-A1102. 二叉树-翻转 层序、中序输出
- 翻转二叉树
- 翻转二叉树
- 翻转二叉树(Invert Binary Tree)