您的位置:首页 > 其它

175. 翻转二叉树

2018-03-12 21:10 190 查看

175. 翻转二叉树 

 描述
 笔记
 数据
 评测
翻转一棵二叉树您在真实的面试中是否遇到过这个题? Yes样例
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 = root->left;
root->left = root->right;
root->right = temp;
if(root->left!=NULL)invertBinaryTree(root->left);
if(root->right!=NULL)invertBinaryTree(root->right);
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: