算法:Flatten Binary Tree to Linked List
来源:互联网 发布:js createelement 编辑:程序博客网 时间:2024/05/17 18:26
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
\
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*> node_stack; node_stack.push(root); TreeNode* new_root=NULL; TreeNode* cur=NULL; TreeNode* next=NULL; while(!node_stack.empty()){ cur=node_stack.top(); node_stack.pop(); if(cur->right!=NULL) node_stack.push(cur->right); if(cur->left!=NULL) node_stack.push(cur->left); if(new_root==NULL){ new_root=cur; next=cur; cur->left=NULL; }else { next->right=cur; next->left=NULL; next=cur; } } root=new_root; }};
0 0
- 算法: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
- 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
- Socket编程之Select
- mysql基础总结(一)
- c#单纯数字数组转换为ASCII码
- [React Native]去掉WebStorm中黄色警告
- 微信 心跳包机制
- 算法:Flatten Binary Tree to Linked List
- Linux命令行手动配置无线wiff网络 iwconfig
- mysql实现row_number() over
- C语言指针学习笔记二
- RealServer快速安装教程
- java基础(六)ArrayList、LinkedList、Vector的区别
- mysql中" ' "和 " ` "的区别
- HTML块级元素和行级元素
- Spring MultipartFile 上传 与 excel处理