题目:将一棵二叉树履平成一个类似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. Python之异常处理(Day27)

    一.错误和异常 part1: 1.语法错误(这种错误,根本过不了python解释器的语法检测,必须在程序执行前就改正) #语法错误示范一 if #语法错误示范二 def test: pass #语法错 ...

  2. form 表单<input type="button" value="登录" onclick="loginSubmit ()"/> 点击提示 Uncaught TypeError: loginSubmit is not a function

    在网上搜了一堆东东,仔细看了一下,再加上实验,发现原因出在<form>中. <form method="post"> <button type=&qu ...

  3. PAT 天梯赛 L1-023. 输出GPLT 【水】

    题目链接 https://www.patest.cn/contests/gplt/L1-023 AC代码 #include <iostream> #include <cstdio&g ...

  4. php任务管理器 —— Jobby

    通过一个主crontab任务去维护别的任务 自定义的计划任务完全由PHP编写 任务的执行计划时间表设置与crontab的时间表设置语法一致 在指定的时间内只会运行一个任务 邮件告警异常退出任务 在ro ...

  5. Ubuntu 12.04下boost库的交叉编译

    oost Ver: 1.55.0Compiler : GNU gcc 4.6 for ARM 1. 确保ARM编译成功安装,并配置好环境变量.2. 解压boost压缩包 3. 进入目录执行./boot ...

  6. STM32探秘 之FSMC

    源:STM32探秘 之FSMC STM32 FSMC总线深入研究

  7. 使用JDK将tomcat变成https访问

    1,今日JDK目录,执行命令 keytool -genkeypair -alias "tomcat" -keyalg "RSA" -keystore " ...

  8. SQL单行函数和多行函数

    单行函数和多行函数示意图: 单行函数分为五种类型:字符函数.数值函数.日期函数.转换函数.通用函数 单行函数: --大小写控制函数 select lower('Hello World') 转小写, u ...

  9. VMware VIC

    vSphere Integrated Containers - a short intro High-Level view of VCH Networking vSphere Integrated C ...

  10. vRO Extend VirtualDisk Workflow

    https://vbombarded.wordpress.com/2015/02/20/vrealize-orchestrator-extend-virtual-disk-workflow/ var ...