题目:将一棵二叉树履平成一个类似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. Ubuntu安装配置samba

    一. samba的安装: sudo apt-get insall sambasudo apt-get install smbfs 二. 创建共享目录: mkdir /home/chars/shares ...

  2. python Selenium库的使用

    一.什么是Selenium selenium 是一套完整的web应用程序测试系统,包含了测试的录制(selenium IDE),编写及运行(Selenium Remote Control)和测试的并行 ...

  3. iOS 9 的新功能 universal links

    什么是 universal links: (通用链接) 一种能够方便的通过传统 HTTP 链接来启动 APP, 使用相同的网址打开web page和 APP的方式. 第一点,链接打开网址 顾名思义 第 ...

  4. transition失效问题

    关于transition,css教程中有一个很简单的例子: <!DOCTYPE html> <html> <head> <meta charset=" ...

  5. linux中获取堆栈空间大小的方法

    #include   <stdio.h> #include   <stdlib.h> #include   <sys/time.h> #include   < ...

  6. SQL性能调优策略

    1.建立索引 2.避免全表扫描 避免使用is null, is not null,这样写会放弃该字段的索引. 如果会出现这种情况,尽量在设计表的时候设置默认值 比较操作符中!= <>等避免 ...

  7. gcc编译c、c++入门

    一.c语言 1.在当前目录下新建c文件 $:vim hello.c 2.按i进入编辑模式.按esc退出编辑模式,输入源代码 #include <stdio.h> int main(void ...

  8. eclipse maven 项目 maven build 无反应

    eclipse maven 项目 使用maven build ,clean 等命令均无反应,控制台无任何输出 1.打开Window --> Preferences --> Java --& ...

  9. mysql错误总结-ERROR 1067 (42000): Invalid default value for TIMESTAMP

    1. ERROR 1067 (42000): Invalid default value for 'FAILD_TIME'   (对TIMESTAMP  类型的子段如果不设置缺省值或没有标志not n ...

  10. 算法总结之 构造数组MaxTree

    一个数组的MaxTree定义如下: 数组必须没有重复元素 MaxTree是一颗二叉树,数组的每一个值对应一个二叉树的节点 包括MaxTre树在内且在其中的每一颗子树上,值最大的节点都是树的头 给定一个 ...