标题: Flatten Binary Tree to Linked List
通过率: 28.7%
难度: 中等

Given a binary tree, flatten it to a linked list in-place.

For example,
Given

         1/ \2   5/ \   \3   4   6

The flattened tree should look like:

   1\2\3\4\5\6

click to show hints.

Hints:

If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-order traversal.

本题就是把一颗树变成退化树

都是只有右孩子,那么需要操作的就是找到左孩子的最有节点,将root的右树连接到左孩子的最右节点,然后root的右孩子指向左孩子,root的左孩子置空

看代码具体操作:

 1 /**
 2  * Definition for binary tree
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public void flatten(TreeNode root) {
12        while(root!=null){
13            if(root.left!=null){
14             TreeNode tmp=root.left;
15             while(tmp.right!=null)
16                 tmp=tmp.right;
17             tmp.right=root.right;
18             root.right=root.left;
19             root.left=null;
20            }
21             root=root.right;
22        }
23     }
24 }

转载于:https://www.cnblogs.com/pkuYang/p/4333946.html

leetcode------Flatten Binary Tree to Linked List相关推荐

  1. LeetCode: Flatten Binary Tree to Linked List

    LeetCode: Flatten Binary Tree to Linked List LeetCode: Flatten Binary Tree to Linked List Given a bi ...

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

  3. [LeetCode] Flatten Binary Tree to Linked List

    Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1/ ...

  4. LeetCode Flatten Binary Tree to Linked List (dfs)

    问题:给出一个二叉树,按照前序遍历方式转变成一个链表. 思路: 将节点的右子树链接到左子树的最右结点,同时将结点的左子树赋值给当前结点的右孩子结点.依次这样处理即可. 具体代码参考: https:// ...

  5. [leetcode]Flatten Binary Tree to Linked List

    class Solution { public://目的是将binary tree变成前序遍历的list,但是变换过程是后序遍历void flatten(TreeNode *root, TreeNod ...

  6. Flatten Binary Tree to Linked List - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Flatten Binary Tree to Linked List - LeetCode 注意点 不要访问空结点 val会有负值 解法 解法一:递归,D ...

  7. LeetCode - Medium - 114. Flatten Binary Tree to Linked List

    Topic Tree Depth-first Search Description https://leetcode.com/problems/flatten-binary-tree-to-linke ...

  8. Leetcode:114. Flatten Binary Tree to Linked List

    题目 Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: ...

  9. Java for LeetCode 114 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 The ...

  10. leetcode [114]Flatten Binary Tree to Linked List

    Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1/ ...

最新文章

  1. 2017暑期挖坑计划(持续更新中~)
  2. 无法访问http,会强制跳到https
  3. 数据库相关的系统巡检参考项
  4. Android在ListView中嵌套一个GridView时只显示一行的原因及解决方法
  5. Socket网络编程【获取本机IP】
  6. 如何定制日历控件显示的星期文字
  7. Java集合源码学习(4)HashSet
  8. ClassLoader背景知识
  9. Normalizing Flows Tutorial(标准化流教程)第一部分
  10. cognos report上钻下钻报表处理方法(2)
  11. setCompoundDrawables和setCompoundDrawablesWithIntrinsicBounds区别
  12. α-β剪枝算法学习寄(蒟蒻向,巨佬勿入)
  13. Word导出PDF后,PDF没有生成Word中对应的目录
  14. 浅析Chrome Packaged Apps
  15. ESP32-cam使用-智能家居云端视频监控实现
  16. 无线路由器常用的五种工作模式详细介绍
  17. AndroidManifest.xml中常用属性及含义
  18. 机器人及无人驾驶领域真实人才供需状况如何?企业真正需要什么样的人才?
  19. 11家共享单车可通过支付宝免押骑车,这种省钱的方法你get了吗?
  20. 学好SEO需要掌握哪些知识要点?

热门文章

  1. linux curl获取头部信息,curl 命令如何获取 http header 返回的响应消息头
  2. php 修改文件的权限_php修改文件权限
  3. stm32for循环几个机械周期_带你了解包装机械设备的可调度性分析
  4. c语言字符串strl复制转换,C语言字符串基础学习
  5. java 默认为空的注解,错误注解的字段设置一个默认的空值
  6. kivy python 读取oracle数据库_python app (kivy)-与小型数据库连接,实现注册登录操作...
  7. JS实现键盘事件上下翻页
  8. alexnet的loss在一个epoch内没有稳定下降_深度学习中的双下降现象
  9. SIFT特征原理与理解
  10. MySQL主从数据同步延时分析