leetcode_c++:树: Flatten Binary Tree to Linked List(114)
来源:互联网 发布:hbo美剧推荐知乎 编辑:程序博客网 时间:2024/05/29 07:51
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
1 / \ 2 5 / \ \ 3 4 6
he flattened tree should look like:
1 \ 2 \ 3 \ 4 \ 5 \ 6
把一棵树变成一长条。
(C++)DFS,先把左右两边的子树处理好,再合并。
算法
class Solution {public: void flatten(TreeNode *root) { vector<TreeNode*> v, stack; stack.push_back(root); while(stack.size()>0){ TreeNode* node = stack.back(); stack.pop_back(); v.push_back(node); if (node && node->right){ stack.push_back(node->right); } if (node && node->left){ stack.push_back(node->left); } } v.push_back(NULL); for(int i=0; i<v.size(); i++){ if (v[i]){ v[i]->left = NULL; v[i]->right = v[i+1]; } } }};
0 0
- leetcode_c++:树: Flatten Binary Tree to Linked List(114)
- 114Flatten Binary Tree to Linked List
- 114- Flatten Binary Tree to Linked List
- LeetCode(114) 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
- [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
- 绑定服务调用本地服务中的方法
- 二叉树的最大距离
- 绑定服务调用远程服务中的方法
- Activiti 使用spring 进行统一事务管理
- Android中内容提供者ContentProvider的详解
- leetcode_c++:树: Flatten Binary Tree to Linked List(114)
- Android中四大组件总结
- 安卓手机下拉状态栏的代码实现
- MD5加密之提取文件的MD5特征码
- 设计模式-单例模式
- MD5加密之加密字符串
- Android应用内多进程分析和研究
- 将博客搬至CSDN
- Express开发框架的安装与配置