/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* upsideDownBinaryTree(TreeNode* root) {
if(!root)
return nullptr;
if(root&&!root->left)
return root;
TreeNode* p=root,*q=nullptr,*pre=nullptr;
while(p){
if(p->left&&!p->left->left){
pre = p;
q = p->left;
}
p=p->left;
}
if(q==nullptr)
return nullptr;
q->left=pre->right;
pre->left=nullptr;
pre->right=nullptr;
q->right=upsideDownBinaryTree(root);
return q;
}
};
156. 上下翻转二叉树
最新推荐文章于 2023-10-16 13:05:51 发布