首页 > 其他 > 详细

LeetCode题解之 Increasing Order Search Tree

时间:2019-02-24 14:53:48      阅读:132      评论:0      收藏:0      [点我收藏+]

1、题目描述

技术分享图片

2/问题分析

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

 

3、代码

 1 TreeNode* increasingBST(TreeNode* root) {
 2         if (root == NULL)
 3             return NULL;
 4         vector<int> v;
 5         inorder(root,v);
 6         
 7         TreeNode* dummy = new TreeNode(0);
 8         TreeNode *p = dummy;
 9         for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
10             TreeNode *tmp = new TreeNode(*it);
11             p->right = tmp;
12             p = p->right;
13         }
14         
15         return dummy->right;
16     }
17     
18     void inorder(TreeNode *root, vector<int> &v)
19     {
20         if (root == NULL)
21             return ;
22         inorder(root->left, v);
23         v.push_back(root->val);
24         inorder(root->right,v);
25     }

 

LeetCode题解之 Increasing Order Search Tree

原文:https://www.cnblogs.com/wangxiaoyong/p/10426099.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!