题目链接
https://leetcode.com/problems/flatten-binary-tree-to-linked-list/
代码
class Solution {
public:
void flatten(TreeNode* root) {
TreeNode* cur = NULL;
dfs(root, cur);
}
void dfs(TreeNode* parent, TreeNode*& cur) {
if (parent == NULL) {
return;
}
TreeNode* right = parent->right;
TreeNode* left = parent->left;
if (cur == NULL) {
cur = parent;
cur->right = NULL;
cur->left = NULL;
} else {
cur->right = parent;
cur->left = NULL;
cur = parent;
}
dfs(left, cur);
dfs(right, cur);
}
};
网友评论