Invert Binary Tree
生活随笔
收集整理的這篇文章主要介紹了
Invert Binary Tree
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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; }總結
以上是生活随笔為你收集整理的Invert Binary Tree的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何彻底删除sql2008
- 下一篇: 苹果2023款Mac mini发布:搭载