Flatten Binary Tree to Linked List--LeetCode
来源:互联网 发布:电子海报制作软件 编辑:程序博客网 时间:2024/06/06 16:57
题目:
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like:
1 \ 2 \ 3 \ 4 \ 5 \ 6
在实现的时候需要注意,最后的连接需要注意查看是否为空的操作。
void helper_list(BinTree*& root,BinTree*& head,BinTree*& tail){if(root == NULL ||(root->right == NULL && root->left == NULL)){head = root;tail = root;return ;} head = root;BinTree* left_head = root->left;head->left = NULL;BinTree* left_tail = NULL;BinTree* right_head = root->right;BinTree* right_tail = NULL;helper_list(left_head,left_head,left_tail);helper_list(right_head,right_head,right_tail);if(left_head != NULL)head->right = left_head;elsehead->right = right_head;if(left_tail != NULL)left_tail->right = right_head;if(right_tail != NULL)tail = right_tail;elsetail = left_tail;}BinTree* LinkedList(BinTree* root){if(root == NULL)return NULL;BinTree * head=NULL,*tail=NULL;helper_list(root,head,tail);return head;}
0 0
- LeetCode: Flatten Binary Tree to Linked List
- LeetCode Flatten Binary Tree to Linked List
- LeetCode: Flatten Binary Tree to Linked List
- [Leetcode] Flatten Binary Tree to Linked List
- [LeetCode] Flatten Binary Tree to Linked List
- Leetcode: Flatten Binary Tree to Linked List
- leetcode Flatten Binary Tree to Linked List
- LeetCode Flatten Binary Tree to Linked List
- LeetCode - Flatten Binary Tree to Linked List
- 【leetcode】Flatten Binary Tree to Linked List
- [LeetCode]Flatten Binary Tree to Linked List
- [Leetcode]Flatten Binary Tree to Linked List
- [leetcode]Flatten Binary Tree to Linked List
- LeetCode-Flatten Binary Tree to Linked List
- [leetcode] Flatten Binary Tree to Linked List
- LeetCode - Flatten Binary Tree to Linked List
- LeetCode:Flatten Binary Tree to Linked List
- [LeetCode] Flatten Binary Tree to Linked List
- Android 消息机制
- 读《C语言深度解剖》1
- 《实体解析与信息质量》-1.2.7 复习题
- Android Studio 慢吗?No!!你还不懂她···
- 樱园三舍319
- Flatten Binary Tree to Linked List--LeetCode
- IOS Method swizzling
- 网络协议规范大全
- 当前主流的备份技术与备份模式
- navicat10.1.7注册码
- 光流Optical Flow介绍与OpenCV实现
- jquery,跨域访问
- app hybrid开发的一点思考
- Xcode6后手动添加.pch文件,与其常遇到的问题