Leetcode 897. 递增顺序搜索树

/**
 * 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 dfs(TreeNode* T, TreeNode* &pre, TreeNode* &NT)
    {
        if(T == nullptr) return;
        dfs(T->left, pre, NT);
        if(pre != nullptr) pre->right = T;
        else NT = T;
        T->left = nullptr;
        pre = T;
        dfs(T->right, pre, NT);
    }
    TreeNode* increasingBST(TreeNode* root) {
        TreeNode* pre = nullptr;
        TreeNode* NT = nullptr;
        dfs(root, pre, NT);
        pre->right = nullptr;
        return NT;
    }
};

 

上一篇:c++ vector模拟实现


下一篇:C++ 笔记(28)— C++ 中 NULL和 nullptr 的区别