360面试题——搜索二叉树转换成有序的双向链表
2016-10-09 16:46
417 查看
360面试题——(不允许创建新的节点)搜索二叉树转换成有序的双向链表
这道题有两种思路,一种是创建节点,中序遍历搜索二叉树,然后链表尾插。第二种是线索化二叉树的思想(思路二才是符合题目要求的)
核心代码
<strong><span style="font-size:18px;"> Node* TreeToList()
{
if(_root == NULL)
{
return NULL;
}
Node* prev = NULL;
_TreeToList(_root,prev);
//寻找头节点
while(_root->_left)
{
_root = _root->_left;
}
return _root;
}</span></strong>
“test.cpp”
<strong><span style="font-size:18px;">#include<iostream>
using namespace std;
template<class T>
struct BSTreeRNode
{
T _key;
BSTreeRNode<T>* _left;
BSTreeRNode<T>* _right;
BSTreeRNode(const T& key)
:_key(key)
,_left(NULL)
,_right(NULL)
{}
};
template<class T>
class BSTreeR
{
typedef BSTreeRNode<T> Node;
public:
BSTreeR()
:_root(NULL)
{}
public:
bool Insert(const T& key)
{
return _Insert(_root,key);
}
void InOrder()
{
_InOrder(_root);
cout<<endl;
}
Node* TreeToList()
{
if(_root == NULL)
{
return NULL;
}
Node* prev = NULL;
_TreeToList(_root,prev);
//寻找头节点
while(_root->_left)
{
_root = _root->_left;
}
return _root;
}
void Display()
{
Node* cur = _root;
while(cur)
{
cout<<cur->_key<<" ";
cur = cur->_right;
}
cout<<endl;
}
private:
void _TreeToList(Node* cur,Node*& prev)
{
if(cur == NULL)
{
return;
}
_TreeToList(cur->_left,prev);
cur->_left = prev;
if(prev)
{
prev->_right = cur;
}
prev = cur;
_TreeToList(cur->_right,prev);
}
void _InOrder(Node* root)
{
if(root == NULL)
{
return;
}
_InOrder(root->_left);
cout<<root->_key<<" ";
_InOrder(root->_right);
}
bool _Insert(Node*& root,const T& key)
{
if(root == NULL)
{
root = new Node(key);
return true;
}
if(root->_key > key)
{
_Insert(root->_left,key);
}
else if(root->_key < key)
{
_Insert(root->_right,key);
}
else//root->_key == key
{
return false;
}
return true;
}
private:
Node* _root;
};
void test()
{
BSTreeR<int> bst;
bst.Insert(5);
bst.Insert(9);
bst.Insert(0);
bst.Insert(6);
bst.Insert(8);
bst.Insert(2);
bst.InOrder();
bst.TreeToList();
bst.Display();
}
int main()
{
test();
return 0;
}</span></strong>
这道题有两种思路,一种是创建节点,中序遍历搜索二叉树,然后链表尾插。第二种是线索化二叉树的思想(思路二才是符合题目要求的)
核心代码
<strong><span style="font-size:18px;"> Node* TreeToList()
{
if(_root == NULL)
{
return NULL;
}
Node* prev = NULL;
_TreeToList(_root,prev);
//寻找头节点
while(_root->_left)
{
_root = _root->_left;
}
return _root;
}</span></strong>
<strong><span style="font-size:18px;"> void _TreeToList(Node* cur,Node*& prev) { if(cur == NULL) { return; } _TreeToList(cur->_left,prev); cur->_left = prev; if(prev) { prev->_right = cur; } prev = cur; _TreeToList(cur->_right,prev); }</span></strong>
“test.cpp”
<strong><span style="font-size:18px;">#include<iostream>
using namespace std;
template<class T>
struct BSTreeRNode
{
T _key;
BSTreeRNode<T>* _left;
BSTreeRNode<T>* _right;
BSTreeRNode(const T& key)
:_key(key)
,_left(NULL)
,_right(NULL)
{}
};
template<class T>
class BSTreeR
{
typedef BSTreeRNode<T> Node;
public:
BSTreeR()
:_root(NULL)
{}
public:
bool Insert(const T& key)
{
return _Insert(_root,key);
}
void InOrder()
{
_InOrder(_root);
cout<<endl;
}
Node* TreeToList()
{
if(_root == NULL)
{
return NULL;
}
Node* prev = NULL;
_TreeToList(_root,prev);
//寻找头节点
while(_root->_left)
{
_root = _root->_left;
}
return _root;
}
void Display()
{
Node* cur = _root;
while(cur)
{
cout<<cur->_key<<" ";
cur = cur->_right;
}
cout<<endl;
}
private:
void _TreeToList(Node* cur,Node*& prev)
{
if(cur == NULL)
{
return;
}
_TreeToList(cur->_left,prev);
cur->_left = prev;
if(prev)
{
prev->_right = cur;
}
prev = cur;
_TreeToList(cur->_right,prev);
}
void _InOrder(Node* root)
{
if(root == NULL)
{
return;
}
_InOrder(root->_left);
cout<<root->_key<<" ";
_InOrder(root->_right);
}
bool _Insert(Node*& root,const T& key)
{
if(root == NULL)
{
root = new Node(key);
return true;
}
if(root->_key > key)
{
_Insert(root->_left,key);
}
else if(root->_key < key)
{
_Insert(root->_right,key);
}
else//root->_key == key
{
return false;
}
return true;
}
private:
Node* _root;
};
void test()
{
BSTreeR<int> bst;
bst.Insert(5);
bst.Insert(9);
bst.Insert(0);
bst.Insert(6);
bst.Insert(8);
bst.Insert(2);
bst.InOrder();
bst.TreeToList();
bst.Display();
}
int main()
{
test();
return 0;
}</span></strong>
相关文章推荐
- 如何将搜索二叉树转换成有序双向链表
- 将搜索二叉树转换成有序的双向链表
- 读剑指offer有感--搜索二叉树转化为有序双向链表
- 搜索二叉树转为有序双向链表
- 将搜索二叉树转换成双向链表
- 面试题27:二叉搜索树转换为有序双向链表
- 有序单向链表转换为平衡搜索二叉树
- 如何搜索二叉树转换为双向链表
- 静态变量的应用--将二叉排序树转换为有序的双向链表输出
- 面试100题系列之1将查找二叉树转换成有序的双向链表
- 有序二叉树转为有序双向链表
- [程序猿面试题精选100称号]1.转变为一个二叉搜索树有序双向链表
- 【面试题】二叉搜索树转换双向链表
- 二叉搜索树(搜索二叉树)转换成一个双向链表
- Cracking-- 17.13 将二叉树转换成双向链表
- 普通二叉树转换成搜索二叉树
- 面试题28:二叉树搜索的后序遍历序列
- 链表节点面试100题系列之1将查找二叉树转换成有序的双向链表
- 面试题:搜索二叉树转双向链表
- 面试题1:二叉树变双向链表