题解 | #翻转牛群结构#
翻转牛群结构
https://www.nowcoder.com/practice/f17e1aa5329f492f9107cbe303222ad6
1.考察知识点
二叉树、递归
2.编程语言
C
3.解题思路:
递归调用左右子树,使用中间节点变量,交换左右节点,返回即可
4.完整代码
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param root TreeNode类 * @return TreeNode类 */ struct TreeNode* invertTree(struct TreeNode* root ) { // write code here if(root==NULL) return root; struct TreeNode* left = invertTree(root->left); struct TreeNode* right = invertTree(root->right); root->left =right; root->right = left; return root; }#面试高频TOP202#