114. 二叉树展开为链表
程序员文章站
2024-03-26 14:12:29
...
题解:特殊的后序遍历,右->左->中
https://leetcode.wang/leetcode-114-Flatten-Binary-Tree-to-Linked-List.html
/**
* * 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:
void flatten(TreeNode* root) {
TreeNode* pre = nullptr;
back_order(root, pre);
}
void back_order(TreeNode* root, TreeNode*& pre) {
if(root == nullptr) {
return;
}
back_order(root->right, pre);
back_order(root->left, pre);
root->right = pre;
root->left = nullptr;
pre = root;
}
};
上一篇: 114. 二叉树展开为链表
下一篇: 图像矫正去畸变