面试题27. 二叉树的镜像
生活随笔
收集整理的這篇文章主要介紹了
面试题27. 二叉树的镜像
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
2020-03-21
1.題目描述
二叉樹的鏡像2.題解
又是一個(gè)樹的問題,使用遞歸比較好求解3.代碼
#include <iostream> #include <algorithm> using namespace std;// 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* mirrorTree(TreeNode* root) {if (!root) return NULL;TreeNode* tmp = (TreeNode*)malloc(sizeof(TreeNode));if (tmp){tmp->val=root->val;tmp->left=mirrorTree(root->right);tmp->right=mirrorTree(root->left);}return tmp;} };int main(){Solution s;return 0; }總結(jié)
以上是生活随笔為你收集整理的面试题27. 二叉树的镜像的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: final关键字细节
- 下一篇: CriminalIntent项目的强大完