码迷,mamicode.com
首页 > 其他好文 > 详细

算法题——二叉树转换为双向链表

时间:2014-08-21 13:15:54      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   ar   div   log   算法   

 1 BSTreeNode* ConvertNode(BSTreeNode* pNode, bool asRight)
 2 {
 3       if(!pNode)
 4             return NULL;
 5 
 6       BSTreeNode *pLeft = NULL;
 7       BSTreeNode *pRight = NULL;
 8 
 9       // Convert the left sub-tree
10       if(pNode->m_pLeft)
11             pLeft = ConvertNode(pNode->m_pLeft, false);
12 
13       // Connect the greatest node in the left sub-tree to the current node
14       if(pLeft)
15       {
16             pLeft->m_pRight = pNode;
17             pNode->m_pLeft = pLeft;
18       }
19 
20       // Convert the right sub-tree
21       if(pNode->m_pRight)
22             pRight = ConvertNode(pNode->m_pRight, true);
23 
24       // Connect the least node in the right sub-tree to the current node
25       if(pRight)
26       {
27             pNode->m_pRight = pRight;
28             pRight->m_pLeft = pNode;
29       }
30 
31       BSTreeNode *pTemp = pNode;
32 
33       // If the current node is the right child of its parent, 
34       // return the least node in the tree whose root is the current node
35       if(asRight)
36       {
37             while(pTemp->m_pLeft)
38                   pTemp = pTemp->m_pLeft;
39       }
40       // If the current node is the left child of its parent, 
41       // return the greatest node in the tree whose root is the current node
42       else
43       {
44             while(pTemp->m_pRight)
45                   pTemp = pTemp->m_pRight;
46       }
47  
48       return pTemp;
49 }

 

算法题——二叉树转换为双向链表,布布扣,bubuko.com

算法题——二叉树转换为双向链表

标签:style   blog   color   os   ar   div   log   算法   

原文地址:http://www.cnblogs.com/qieerbushejinshikelou/p/3926916.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!