[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 解析 通过递归实现:可以用先序遍历, ...
随机推荐
- C#如何使用结构化异常处理
Knowledge Base: Chinese (Simplified) 如何使用 Visual C# .NET 和 Visual C# 2005 中的结构化异常处理文章ID: 816157 最近更新 ...
- gitattributes手册
gitattributes(5) Manual Page 1.gitattributes是什么? gitattributes用于定义每个路径的属性. 其语法是:pattern attr1 attr2 ...
- Mapinfo修改道路方向
在mapinfp工具管理添加插件Reverse Line Direction,就可以修改道路方向 插件
- 系统非正常关机启动后出现:an error occurred during the file system
现象描述: 1.系统ssh登录报Too many open files in system,系统登录不进去,就直接强制关机了,开机后出现(2)的错误: 由于文件描述符用完了,需要把fs.file-ma ...
- Chrome 的 Rendering 监听器
在研究动画优化时,有被安利一款这个...啥,额,就是,唔...就是一个能让我们看到动画卡不卡的监听器 火狐的“高亮重绘区域”个人感觉并不好用,而 Safari 竟然没找到,而 IE11 也没有(公司的 ...
- gst-rtsp-server编译测试
最近在做dm368的开发,打算在368上移植个gst-rtsp-server.先在电脑上折腾了一天,终于是可以运行了. 我的虚拟机上早先已经安装了gstreamer-0.10(gstreamer版本太 ...
- 《机器学习实战第7章:利用AdaBoost元算法提高分类性能》
import numpy as np import matplotlib.pyplot as plt def loadSimpData(): dataMat = np.matrix([[1., 2.1 ...
- 我到 vim 配置文件---------修改从---http://www.cnblogs.com/ma6174/archive/2011/12/10/2283393.html
""""""""""""""""&quo ...
- jsp中的basePath和path(绝对路径 相对路径)
在JSP中的如果使用 "相对路径" 则有 可能会出现问题. 因为 网页中的 "相对路径" , 他是相对于 "URL请求的地址" 去寻找资源. ...
- JAVA获取Spring上下文
1. 添加监听 public class SpringContextListener implements ServletContextListener { //获取spring注入的bean对象 p ...