5.1.11—二叉树的遍历—Flatten Binary Tree to Linked List
2017-08-07 21:34
507 查看
描述
Given a binary tree, flaen it to a linked list in-place.
For example, Given
1
/ \
2 5
/ \ \
3 4 6
e flaened tree should look like:
1
\
2
\
3
\
4
\
5
\
6
Given a binary tree, flaen it to a linked list in-place.
For example, Given
1
/ \
2 5
/ \ \
3 4 6
e flaened tree should look like:
1
\
2
\
3
\
4
\
5
\
6
#include "BinaryTree.h" #include<vector> using namespace std; //===将一棵二叉树就地转化为单链表---递归版本 void FlatBinTreeToLinkList(BinaryTreeNode *proot) { if (!proot) return; if (proot->m_pLeft) FlatBinTreeToLinkList(proot->m_pLeft); if (proot->m_pRight) FlatBinTreeToLinkList(proot->m_pRight); if (proot->m_pLeft == NULL) return; BinaryTreeNode *temp = proot->m_pRight; BinaryTreeNode *p = proot->m_pLeft; while (p->m_pRight) p = p->m_pRight; p->m_pRight = temp; proot->m_pRight = proot->m_pLeft; proot->m_pLeft = NULL; } //===将一棵二叉树就地转化为单链表---迭代版本 void FlatBinTreeToLinkList1(BinaryTreeNode *proot) { if (!proot) return; BinaryTreeNode *cur = proot; while (cur) { if (cur->m_pLeft) { BinaryTreeNode *p_left = cur->m_pLeft; BinaryTreeNode *p_right = cur->m_pRight; while (p_left->m_pRight) p_left = p_left->m_pRight; cur->m_pRight = cur->m_pLeft; cur->m_pLeft = NULL; p_left->m_pRight = p_right; } cur = cur->m_pRight; } } // ====================测试代码==================== // 8 // 6 10 // 5 7 9 11 int main() { //=== BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8); BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6); BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10); BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5); BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7); BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9); BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11); ConnectTreeNodes(pNode8, pNode6,pNode10); ConnectTreeNodes(pNode6, pNode5, pNode7); ConnectTreeNodes(pNode10, pNode9, pNode11); //=== //PrintTree(pNode8); //=== //FlatBinTreeToLinkList(pNode8); //PrintTree(pNode8); //=== FlatBinTreeToLinkList1(pNode8); PrintTree(pNode8); DestroyTree(pNode8); }
相关文章推荐
- 【遍历二叉树】11把二叉树转换成前序遍历的链表【Flatten Binary Tree to Linked List】
- 114 Flatten Binary Tree to Linked List 二叉树到单链表的扁平化处理
- 【LeetCode-面试算法经典-Java实现】【114-Flatten Binary Tree to Linked List(二叉树转单链表)】
- [C++]LeetCode: 102 Flatten Binary Tree to Linked List (二叉树转前序链表)
- [LintCode] Flatten Binary Tree to Linked List 将二叉树展开成链表
- [LeetCode] Flatten Binary Tree to Linked List 将二叉树展开成链表 C++
- [LeetCode]Flatten Binary Tree to Linked List(二叉树转链表)
- Flatten Binary Tree to Linked List:二叉树变更结构
- [LeetCode] Flatten Binary Tree to Linked List 将二叉树展开成链表
- 把二叉树精简成单链表 Flatten Binary Tree to Linked List
- LeetCode | Flatten Binary Tree to Linked List(二叉树转化成链表)
- LeetCode OJ:Flatten Binary Tree to Linked List(捋平二叉树)
- Flatten Binary Tree to Linked List 将二叉树转换为链表
- LeetCode OJ 之 Flatten Binary Tree to Linked List (把二叉树转换成链表)
- Flatten Binary Tree to Linked List 二叉树变成链表@LeetCode,
- LeetCode:114_Flatten Binary Tree to Linked List | 将一棵二叉树变成链表的形式 | Medium
- 5.1.11 Flatten Binary Tree to Linked List
- LeetCode Flatten Binary Tree to Linked List 将二叉树展开成链表
- lintcode flatten-binary-tree-to-linked-list 将二叉树拆成链表 前序扁平化 中序扁平化
- 【leetcode 二叉树摊直】Flatten Binary Tree to Linked List