0
点赞
收藏
分享

微信扫一扫

Invert Binary Tree

Ewall_熊猫 2022-12-13 阅读 70


​​https://leetcode.com/problems/invert-binary-tree/​​

Invert a binary tree.

4
/ \
2 7
/ \ / \
1 3 6 9
to
4
/ \
7 2
/ \ / \
9 6 3 1

给出一棵二叉树,求这棵二叉树的镜像。

搬运九章上的实现 ​​http://www.jiuzhang.com/solutions/invert-binary-tree/?source=zhmhw​​

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
struct TreeNode* invertTree(struct TreeNode* root) {
if(root == 0)
return NULL;

struct TreeNode *left = root->left;
struct TreeNode *right = root->right;

root->left = right;
root->right = left;
if(root->left != NULL)
invertTree(root->left);
if(root->right != NULL)
invertTree(root->right);
return root;
}


举报

相关推荐

0 条评论