题目:将一棵二叉树履平成一个类似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的更多相关文章

  1. 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 ...

  2. [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 ...

  3. [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 ...

  4. 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 ...

  5. [leetcode]Flatten Binary Tree to Linked List @ Python

    原题地址:http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ 题意: Given a binary tree, fl ...

  6. 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 ...

  7. LeetCode:114_Flatten Binary Tree to Linked List | 将一棵二叉树变成链表的形式 | Medium

    要求:Given a binary tree, flatten it to a linked list in-place.将二叉树转化为平坦序列的树.比如: 结题思路: 该题有个提示,转化后的树的序列 ...

  8. LeetCode OJ-- Flatten Binary Tree to Linked List **

    https://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ 二叉树的处理,将二叉树转换成类似一个单链表的东东,并且原地. ...

  9. LeetCode 114| Flatten Binary Tree to Linked List(二叉树转化成链表)

    题目 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解析 通过递归实现:可以用先序遍历, ...

随机推荐

  1. 3.6中的range()

    在python3中range()是这样的: >>> range(4) range(0, 4) #额,列表跑哪去了 在之前的python2中是这样的: >>> ran ...

  2. 曾经跳过的坑------JS中对象与结构体的声明和调用

    直接上代码 正确的写法 //同一个ready方法中var viewModel = { // self.projectCode = PROJECT_CODE; BOOKEDCOUNT : 5, TOTA ...

  3. 20145230《java程序设计》 第四次实验报告

    20145230实验4 Android开发基础 实验内容 基于Android Studio开发简单的Android应用并部署测试; 了解Android组件.布局管理器的使用: 掌握Android中事件 ...

  4. axios拦截器/http

    Interceptors //处理请求或响应之前拦截请求或响应. //添加一个请求拦截器 axios.interceptors.request.use(function (config) { //在请 ...

  5. 前段时间说了AssetBundle打包,先设置AssetLabels,再执行打包,但是这样有个弊端就是所有设置了AssetLabels的资源都会打包,这次说说不设置AssetLabels,该如何打包AssetBundle

    BuildPipeline.BuildAssetBundles() 这个函数,有多个重载,一个不用AssetBundleBuild数组,一个需要,如果设置了AssetLabels,那么这时候是不需要的 ...

  6. 机器学习-chapter1机器学习的生态系统

    1.机器学习工作流程 获取->检查探索->清理准备->建模->评估->部署 2.搭建机器学习环境 1..通过安装Python,配置相关环境变量 2.强烈建议直接安装ana ...

  7. 内核编译错误解答(elf_i386错误)

    内核编译错误解答(elf_i386错误) 在编译内核过程中遇到的问题及解决方法: 1.root@org:/usr/src/linux# make menuconfig  *** Unable to f ...

  8. python之Django rest_framework总结

    一.rest api    a.api就是接口         如: - http://www.oldboyedu.com/get_user/                - http://www. ...

  9. 设置浏览器地址栏URL前面显示的图标

    其实很简单,你只做个ico图标,命名为favicon.ico,把它传到你的页面下面. 并在相应的页面里加上代码  在页面<heah></heah>之间加, <link r ...

  10. java对象流(一)

    注意:字节数组流是可以不用关闭的(字符数组流要不要关闭暂时不清楚). 对象流的读数据和写数据方法分别是writeObject(Object o)和readObject(Object o). Objec ...