114. Flatten Binary Tree to Linked List
2016-03-17 19:17
316 查看
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
The flattened tree should look like:
Hints:
If you notice carefully in the flattened tree, each node’s right child points to the next node of a pre-order traversal
For example,
Given
1 / \ 2 5 / \ \ 3 4 6
The flattened tree should look like:
1 \ 2 \ 3 \ 4 \ 5 \ 6
Hints:
If you notice carefully in the flattened tree, each node’s right child points to the next node of a pre-order traversal
/** * 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 && root->right){ TreeNode* t = root->left; while(t->right) t = t->right; t->right = root->right; root->right = NULL; } if(root->left){ root->right = root->left; root->left = NULL; } root= root->right; } } };
相关文章推荐
- Unity中自带时间函数的执行顺序(全部完整版)
- matlab中带参数定积分的求解
- 100万并发连接服务器笔记之处理端口数量受限问题
- Spring定时任务的几种实现及部分模板
- 【Contacts】联系人界面添加联系人统计项
- HDU 1106 排序
- C++ 学习之路(9):(附C++程序的多文件组成)堆栈示例
- ElementWrapper
- android 5.X Toolbar+DrawerLayout实现抽屉菜单
- HTTP 1.1状态代码及其含义
- 【Android】动画 —— view高度渐变的动画
- 读书笔记-Android初学笔记
- 字符串替换replace和replaceAll区别
- 【hdu1175】连连看——dfs(剪枝)
- 100万并发连接服务器笔记之准备篇
- python基础数据类型及内置容器详解
- HDU 1002 java处理大数太好用了
- android app启动过程
- 记录并研究Volley框架中知识点
- express Router对象