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
Hints:

If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal.

void flatten(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(root==NULL) return;
if(root->left == NULL && root->right == NULL) return;
flatten(root->left);
flatten(root->right); TreeNode* tmpright = root->right;
root->right = root->left;
root->left = NULL;
TreeNode* tmp = root;
while(tmp->right)
tmp = tmp->right;
tmp->right = tmpright;
return;
}

最新文章

  1. 解读Unity中的CG编写Shader系列四(unity中的圆角矩形shader)
  2. 谈谈我的编程之路---WAMP(四)
  3. 笑谈Android图表-MPAndroidChart
  4. [cmd]linux 常用命令
  5. 開賣!下集 -- ASP.NET 4.5 專題實務(II)-範例應用與 4.5新功能【VB/C# 雙語法】
  6. linux系统相关的任务[fg、bg、jobs、&、ctrl + z]
  7. the assignment of reading paper
  8. mcrypt.h not found. Please reinstall libmcrypt
  9. Google 分布式关系型数据库 F1
  10. jquery考试纠错笔记.
  11. jqueryl操作dom文档实例
  12. httpd三种MPM的原理剖析
  13. javascript:将URL的参数列表解析为一个对象
  14. Oracle SQL性能优化总结
  15. 在react中使用vis.js
  16. Robot Framework 自动化框架大纲
  17. pip install报错Can't roll back cryptography; was not uninstalled
  18. HDU5985 Lucky Coins 概率dp
  19. Codeforces Round #514 (Div. 2) C. Sequence Transformation
  20. XE2 运行时 item not found的解决办法

热门文章

  1. linux磁盘限额配置:quota命令
  2. hdu5045:带权二分图匹配
  3. Computer Graphics Thinking–texture tiling
  4. Appium 一个测试套件多次启动android应用
  5. php之手机号码查归属地
  6. 使用openCV的静态库编译
  7. jQuery 之$.proxy() 方法
  8. 蓝桥杯 BASIC 24 龟兔赛跑预測(模拟)
  9. JMeter数据库性能测试
  10. javaweb 学习的好地方