Flatten Binary Tree to Linked List
来源:互联网 发布:说说大全软件 编辑:程序博客网 时间:2024/06/17 00:26
Flatten a binary tree to a fake "linked list" in pre-order traversal.
Here we use the right pointer in TreeNode as the nextpointer in ListNode.
样例
1 \ 1 2 / \ \ 2 5 => 3 / \ \ \ 3 4 6 4 \ 5 \ 6
注意
Don't forget to mark the left child of each node to null. Or you will get Time Limit Exceeded or Memory Limit Exceeded.
class Solution {public: void flatten(TreeNode *root) { if(root==NULL) return; if(root->left==NULL&&root->right==NULL) return; if(root->left) flatten(root->left); if(root->right) flatten(root->right); TreeNode* tmp=root->left; if(tmp==NULL) return; while(tmp->right){ tmp=tmp->right; } tmp->right=root->right; root->right=root->left; root->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
- 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
- Leetcode109: Binary Search Tree Iterator
- Deep Learning 2 - 多元线性回归
- 前端进阶试题-CSS篇
- 面试总结
- spring mvc的表单值与jquery互传
- Flatten Binary Tree to Linked List
- 第一篇(详解cocos2d-x中宏CC_DLL)
- 有线无线一体化网络设计原则
- 提高效率的Android Studio插件
- Hive复杂查询时,多层union可以转换为left join,左侧临时维度表生成方式
- 网桥、路由器以及网关三者的区别
- Hive,Pig区别
- 浅谈PHP中IP与整型互相转换
- int main(int argc,char* argv[])详解