C/C++教程

Leetcode 897. 递增顺序搜索树

本文主要是介绍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;
    }
};

 

这篇关于Leetcode 897. 递增顺序搜索树的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!