LeetCode题解之 Increasing Order Search Tree

1、题目描述

LeetCode题解之 Increasing Order Search Tree

2/问题分析

利用中序遍历,然后重新构造树。

3、代码

 TreeNode* increasingBST(TreeNode* root) {
if (root == NULL)
return NULL;
vector<int> v;
inorder(root,v); TreeNode* dummy = new TreeNode();
TreeNode *p = dummy;
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
TreeNode *tmp = new TreeNode(*it);
p->right = tmp;
p = p->right;
} return dummy->right;
} void inorder(TreeNode *root, vector<int> &v)
{
if (root == NULL)
return ;
inorder(root->left, v);
v.push_back(root->val);
inorder(root->right,v);
}
上一篇:LeetCode题解之Diameter of Binary Tree


下一篇:LeetCode题解之 Subtree of Another Tree