[leetcode]_Flatten Binary Tree to Linked List
题目:将一棵二叉树履平成一个类似Linked-list的东西。
思路:该过程类似于二叉树的前序遍历,但是遍历代码,我处理不来参数的变化。没AC。
-------->写的很好的解题博客
参考上述博客,思路很清楚的写出代码:
public void flatten(TreeNode root) {
if(root == null) return;
if(root.left != null){
TreeNode leftNode = root.left;
TreeNode rightNode = root.right;
root.left = null;
root.right = leftNode;
while(leftNode.right != null) leftNode = leftNode.right;
leftNode.right = rightNode;
}
flatten(root.right);
16 }
采用前序遍历错误的代码:留在下方,回头来改正:
public void flatten(TreeNode root) {
if(root == null) return;
TreeNode newRoot = null , head = null;
Stack<TreeNode> s = new Stack<TreeNode>();
while(root != null || !s.isEmpty()){
while(root != null){
//visit node
if(newRoot == null) {
newRoot = new TreeNode(root.val);
head = newRoot;
}else{
newRoot.right = new TreeNode(root.val);
newRoot = newRoot.right;
}
s.push(root);
//visit leftChild
root = root.left;
}
if(!s.isEmpty()){
//visit rightChild
root = s.pop();
root = root.right;
}
}
root = head;
}
[leetcode]_Flatten Binary Tree to Linked List的更多相关文章
- Leetcode:Flatten Binary Tree to Linked List 解题报告
Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. For ex ...
- [LeetCode]Flatten Binary Tree to Linked List题解(二叉树)
Flatten Binary Tree to Linked List: Given a binary tree, flatten it to a linked list in-place. For e ...
- [LeetCode] Flatten Binary Tree to Linked List 将二叉树展开成链表
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...
- LeetCode——Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 4 6 ...
- [leetcode]Flatten Binary Tree to Linked List @ Python
原题地址:http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ 题意: Given a binary tree, fl ...
- LeetCode - Flatten Binary Tree to Linked List
题目: Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 ...
- LeetCode:114_Flatten Binary Tree to Linked List | 将一棵二叉树变成链表的形式 | Medium
要求:Given a binary tree, flatten it to a linked list in-place.将二叉树转化为平坦序列的树.比如: 结题思路: 该题有个提示,转化后的树的序列 ...
- LeetCode OJ-- Flatten Binary Tree to Linked List **
https://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ 二叉树的处理,将二叉树转换成类似一个单链表的东东,并且原地. ...
- LeetCode 114| Flatten Binary Tree to Linked List(二叉树转化成链表)
题目 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解析 通过递归实现:可以用先序遍历, ...
随机推荐
- second application:use an arcgis.com webmap
<!DOCTYPE html> <html> <head> <title>Create a Web Map</title> <meta ...
- Python编程-模块和包
一.模块 1.什么是模块? 一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 2.为何要使用模块? 如果你退出python解释器然后重新进入,那么你之前定义的函 ...
- Qt使用QCustomPlot开发
一.入门 1.下载源文件http://www.qcustomplot.com/: 2.把.cpp和.h放在工程目录下,并将cpp和h加入工程: 3.在.pro中:QT += printsupport: ...
- c++ boost库学习三:实用工具
noncopyable 大家都知道定义一个空类的时候,它实际包含了构造函数,拷贝构造函数,赋值操作符和析构函数等. 这样就很容易产生一个问题,就是当用户调用A a(“^_^") 或者A c= ...
- how to use composer in fiddler
https://www.cnblogs.com/youxin/p/3570310.html http://docs.telerik.com/fiddler/generate-traffic/tasks ...
- BZOJ-1396: 识别子串
后缀自动机+线段树 先建出\(sam\),统计一遍每个点的\(right\)集合大小\(siz\),对于\(siz=1\)的点\(x\),他所代表的子串只会出现一次,设\(y=fa[x]\),则这个点 ...
- SSH或者SSM开发web,mysql数据库,数据库配置文件配置不当~数据库读写数据乱码问题解决办法。
相信,大家都有遇到过在传入一个中文string,debug自己的每一行代码时,都发现始终是没有乱码的(即:排除了,源码文件的编码格式是没问题的),但是数据进入数据库之后就是乱掉了. 那么很明显问题就出 ...
- SpringMVC中响应json数据(异步传送)
1.首先导入3个jar包: jackson-annotations-2.1.5.jar jackson-core-2.1.5.jar jackson-databind-2.1.5.jar JSON所需 ...
- DataX-HDFS(读写)
DataX操作HDFS 读取HDFS 1 快速介绍 HdfsReader提供了读取分布式文件系统数据存储的能力.在底层实现上,HdfsReader获取分布式文件系统上文件的数据,并转换为DataX传输 ...
- 利用Phoenix为HBase创建二级索引
为什么需要Secondary Index 对于Hbase而言,如果想精确地定位到某行记录,唯一的办法是通过rowkey来查询.如果不通过rowkey来查找数据,就必须逐行地比较每一列的值,即全表扫瞄. ...