首页 > 学院 > 开发设计 > 正文

翻转二叉树

2019-11-08 00:59:53
字体:
来源:转载
供稿:网友

题目要求:翻转一颗二叉树,使得该二叉树的所有左右子树发生翻转; 例:这里写图片描述 解:该题可用递归,将左右子树的位置进行翻转置换

void invertBinaryTree(TreeNode *root) { // write your code here //算法设计思路:遍历二叉树的所有节点,然后将它的左右儿子进行位置上的对调 //if the root is empty, return; if(root == NULL) { return; } TreeNode* tmp = root->left; root->left = root->right; root->right = tmp; invertBinaryTree(root->left); invertBinaryTree(root->right); }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表