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; } };
标签:pre,right,TreeNode,897,nullptr,顺序搜索,NT,Leetcode,left 来源: https://www.cnblogs.com/WTSRUVF/p/14701149.html