/** * 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: void core_code(TreeNode* root) { TreeNode* node_temp=root->left;//交换左右子节点 root->left=root->right; root->right=node_temp; if(root->left!=NULL) core_code(root->left);//分别对左右子节点做同一操作 if(root->right!=NULL) core_code(root->right); } TreeNode* invertTree(TreeNode* root) { if(root==NULL)//首先判断边界 return root; core_code(root); return root; } };
分析:
菜了菜了。
原文地址:https://www.cnblogs.com/CJT-blog/p/11302279.html
时间: 2024-10-04 12:52:32