LeetCode114—Flatten Binary Tree to Linked List
来源:互联网 发布:ch341a编程器软件1.29 编辑:程序博客网 时间:2024/06/15 07:12
LeetCode114—Flatten Binary Tree to Linked List
原题
Given a binary tree, flatten it to a linked list in-place.
For example,
Given1 / \ 2 5 / \ \ 3 4 6
The flattened tree should look like:
1
\
2
\
3
\
4
\
5
\
6
分析
最朴素的方法就是先序遍历把节点保存,然后在顺序构造不含左子树的树,网上看到有其他的方法,等学习研究了再更新。
代码
class Solution {private: void dfs(TreeNode*root, vector<TreeNode*>&Nodes) { if (root == NULL) return; Nodes.push_back(root); dfs(root->left, Nodes); dfs(root->right, Nodes); }public: void flatten(TreeNode* root) { if (root == NULL) return; vector<TreeNode*>Nodes; dfs(root, Nodes); int i; root->left = NULL; for (i = 0; i < Nodes.size() - 1; i++) { Nodes[i]->right = Nodes[i + 1]; Nodes[i]->left = NULL; } Nodes[i]->right = NULL; root = Nodes[0]; }};
1 0
- LeetCode114 Flatten Binary Tree to Linked List
- LeetCode114 Flatten Binary Tree to Linked List
- leetcode114~Flatten Binary Tree to Linked List
- LeetCode114 Flatten Binary Tree to Linked List
- leetcode114. Flatten Binary Tree to Linked List
- LeetCode114—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
- C++内存泄漏的检测
- iOS部分知识点小结
- Objective-c 对象与类
- Flash如何插入到PowerPoint2003中
- 安卓开发与分析者的利器--安卓右键工具
- LeetCode114—Flatten Binary Tree to Linked List
- ImageSwitcher 滑动切换图片
- shell编程之环境配置文件
- 将lrzsz工具移植到ARM开发板 ---只通过串口实现window和ARM板之间文件互传
- 为什么String类被设计成final
- zz:Android APP Monkey信息自动收集脚本
- 两种屏幕-世界坐标转换的性能比较
- 堆排序--Java实现
- 窗口风格 :"ModifyStyle函数的用法":