Leetcode147: Flatten Binary Tree to Linked List
2015-11-09 19:13
393 查看
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
The flattened tree should look like:
For example,
Given
1 / \ 2 5 / \ \ 3 4 6
The flattened tree should look like:
1 \ 2 \ 3 \ 4 \ 5 \ 6
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: void flatten(TreeNode* root) { if(!root) return; while(root) { if(root->left) { TreeNode* node = root->left; while(node->right) { node = node->right; } node->right = root->right ; root->right = root->left; root->left = NULL; } root = root->right; } } };
相关文章推荐
- C#中通过三边长判断三角形类型
- Win7 系统上安装SQL Server 2008一步一步图解教程
- STF开源框架之minicap工具
- java数据结构
- 扩号匹配问题(C程序设计进阶第3周)
- creatrepo
- 三道期中考试题总结
- Block
- 联想k920 系统包版本区别和说明
- 2014-2015 ACM-ICPC, Asia Tokyo Regional Contest D题 Space Golf(三分)
- java实现多继承
- test-Markdown0001
- git chekcout
- Swift里类的初始化相关内容(重要)
- MySQL数据库事务隔离级别(Transaction Isolation Level)
- 浅谈 Objective-C 下对象的初始化
- 文件服务器vsftp的配置
- C++实现cascade DPM --- 第二篇
- 三目运算符 的应用
- dyld: Symbol not found: ___NSArray0__