(LeetCode) 114. Flatten Binary Tree to Linked List
来源:互联网 发布:linux哪几种运行级别 编辑:程序博客网 时间:2024/05/17 06:04
114. Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
1 / \ 2 5 / \ \ 3 4 6
The flattened tree should look like:
1 \ 2 \ 3 \ 4 \ 5 \ 6
思路
题干的要求就是按照前序遍历的顺序抹平二叉树,所以只要前序遍历来操作即可。
代码
class Solution {public: void flatten(TreeNode* root) { if (root==NULL) return; stack<TreeNode*> stk; stk.push(root); while(!stk.empty()){ TreeNode* cur = stk.top(); stk.pop(); if (cur->right != NULL) stk.push(cur->right); if (cur->left != NULL) stk.push(cur->left); if (!stk.empty()){ cur->right = stk.top(); } cur->left = NULL; } }};
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
- 深入解析java虚拟机
- Java编写作业检查器
- myeclipse maven配置
- try catch finally 使用说明
- 64位读取注册表与32位的区别
- (LeetCode) 114. Flatten Binary Tree to Linked List
- 02 ExecutorBackend blocked at “UserGroupInformation.doAs”
- 智能客服机器人所需要的技术点
- yarn命令使用
- 170411 PyQt5-控件透明,汇编-call和ret配合、子程序
- css如何清除浮动(一)
- C#
- 【京东—数据挖掘/深度学习】电话面试——2017/04/11
- redis主从模式配置与使用