Flatten Binary Tree to Linked List
来源:互联网 发布:中文手写软件下载 编辑:程序博客网 时间:2024/06/05 13:04
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
https://leetcode.com/problems/flatten-binary-tree-to-linked-list/
方法一:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; *//*struct ListNode{ int val; struct ListNode *next;};*/void firstView(struct TreeNode* root, struct ListNode **list){ if(!root) return; (*list)->val = (int)root; (*list)->next = malloc(sizeof(struct ListNode)); (*list)->next->val = 0; (*list)->next->next = NULL; (*list) = (*list)->next; firstView(root->left, list); firstView(root->right, list);}void flatten(struct TreeNode* root){ struct ListNode *ptmp = NULL; struct ListNode list = {.val=0,.next=NULL}; struct ListNode *p; list.next = malloc(sizeof(struct ListNode)); list.next->val = 0; list.next->next = NULL; p = list.next; firstView(root, &p); p = list.next->next; while(p&&p->val){ root->left = NULL; root->right = (struct TreeNode*)p->val; root = root->right; ptmp = p; p = p->next; free(ptmp); } free(list.next);}
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
- 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
- Flatten Binary Tree to Linked List
- Flatten Binary Tree to Linked List
- LeetCode - 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
- Flatten Binary Tree to Linked List
- Flatten Binary Tree to Linked List
- Flatten Binary Tree to Linked List
- cloudera manager下的hive权限配置
- 微信小程序 - 干货集中营
- zookeeper详解
- android中Sqlite查询的数据返回排序问题
- swift 二维码识别的实现
- Flatten Binary Tree to Linked List
- linux下 gcc编译初学者使用教程
- day11--回顾
- struts 访问servlet api (ServletActionContext 方式)
- Android开发入门——推箱子游戏开发实战(十二)
- CVM ubuntux641604 搭建L2TP VPN
- HDU 1061 Rightmost Digit (求n^n的最低位)
- 利用NodeMcu和matplotlib的温度远程实时测量显示方案
- Android 小问题锦集