标签:
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; * struct TreeNode *left; * struct TreeNode *right; * }; */ struct TreeNode* invertTree(struct TreeNode* root) { struct TreeNode *left, *right; if(root == NULL) return NULL; else if(root->left == NULL && root->right == NULL) return root; else { left = invertTree(root->right); //这里必须保存到left这个临时变量中 right = invertTree(root->left); //否则下一次的翻转会失效 root->left = left; root->right = right; return root; } }
标签:
原文地址:http://www.cnblogs.com/dylqt/p/4881329.html