博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode_question_114 Flatten Binary Tree to Linked List
阅读量:6934 次
发布时间:2019-06-27

本文共 838 字,大约阅读时间需要 2 分钟。

 

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;    }

 

 

转载地址:http://bhgjl.baihongyu.com/

你可能感兴趣的文章
Android使用XML全攻略(2)
查看>>
[USACO08FEB]酒店Hotel
查看>>
卫生纸效果,哈哈
查看>>
mysql导入excel数据
查看>>
Java中写入文件时换行符用"\r\n"、"\n"、"\r"?
查看>>
AIX 命令
查看>>
安装终端服务和终端服务授权,激活终端服务授权
查看>>
朋友,别在降低别人底线或被别人降低底线了!
查看>>
先考学历还是先提升能力?
查看>>
软件项目开发无成熟框架套路之成本代价
查看>>
设计模式(3)-装扮你的类(装饰模式)
查看>>
Android 数字签名学习笔记
查看>>
Linux下Gedit + Gmate ,实用的编辑器
查看>>
OO学习之二——面向对象分析(OOD)的介绍
查看>>
深入python3 (Dive Into Python 3) 在线阅读与下载
查看>>
linux 更改服务的启动顺序
查看>>
【数据结构】除去线性表中的重复数字
查看>>
[原]IE9 DOM的自定义属性问题
查看>>
[CLR via C#]17. 委托
查看>>
Android系统Google Maps开发实例浅析
查看>>