本文共 1034 字,大约阅读时间需要 3 分钟。
返回类型为void,用return NULL是肯定不对的
/** * 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; } swapNode(root); return root; } void swapNode(TreeNode* root){ if (root->left == NULL && root->right == NULL){ return; }else{ TreeNode* temp; temp = root->left; root->left = root->right; root->right = temp; if (root->left != NULL){ invertTree(root->left); } if (root->right != NULL){ invertTree(root->right); } } }};
转载地址:http://wjpbb.baihongyu.com/