[LeetCode226]Invert Binary Tree
生活随笔
收集整理的這篇文章主要介紹了
[LeetCode226]Invert Binary Tree
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目:
Invert a binary tree.
4/ \2 7/ \ / \ 1 3 6 9to
4/ \7 2/ \ / \ 9 6 3 1反轉二叉樹,左右兒子值交換
代碼:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public:TreeNode* invertTree(TreeNode* root) {if (root == NULL)return NULL;TreeNode *tmp = root->left;root->left = root->right;root->right = tmp;invertTree(root->left);invertTree(root->right);return root;} };
?
總結
以上是生活随笔為你收集整理的[LeetCode226]Invert Binary Tree的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 程序员的“认知失调”
- 下一篇: Dev的关于XtraGrid的使用2