返回的root的 left 和right 要设定为null。
class Solution {
private:
TreeNode* ret;
TreeNode* head;
public:
TreeNode* upsideDownBinaryTree(TreeNode* root) {
if(root == NULL) return NULL;
if(head == NULL)
head = root;
// cout<<"root="<<root->val<<endl;
if(root->left == NULL && root->right == NULL) {
ret = root;
return root;
}
TreeNode* newRoot = NULL;
if(root->left)
newRoot = upsideDownBinaryTree(root->left);
if(newRoot != NULL) {
newRoot->left = root->right;
newRoot->right = root;
root->right = NULL;
root->left = NULL;
if(root==head) {
return ret;
}
}
return root;
}
};