leetcode------Flatten Binary Tree to Linked List
标题: | 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.
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相关推荐
- LeetCode: Flatten Binary Tree to Linked List
LeetCode: Flatten Binary Tree to Linked List LeetCode: Flatten Binary Tree to Linked List Given a bi ...
- [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
Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: 1/ ...
- LeetCode Flatten Binary Tree to Linked List (dfs)
问题:给出一个二叉树,按照前序遍历方式转变成一个链表. 思路: 将节点的右子树链接到左子树的最右结点,同时将结点的左子树赋值给当前结点的右孩子结点.依次这样处理即可. 具体代码参考: https:// ...
- [leetcode]Flatten Binary Tree to Linked List
class Solution { public://目的是将binary tree变成前序遍历的list,但是变换过程是后序遍历void flatten(TreeNode *root, TreeNod ...
- Flatten Binary Tree to Linked List - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Flatten Binary Tree to Linked List - LeetCode 注意点 不要访问空结点 val会有负值 解法 解法一:递归,D ...
- LeetCode - Medium - 114. Flatten Binary Tree to Linked List
Topic Tree Depth-first Search Description https://leetcode.com/problems/flatten-binary-tree-to-linke ...
- 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: ...
- 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 ...
- 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/ ...
最新文章
- 2017暑期挖坑计划(持续更新中~)
- 无法访问http,会强制跳到https
- 数据库相关的系统巡检参考项
- Android在ListView中嵌套一个GridView时只显示一行的原因及解决方法
- Socket网络编程【获取本机IP】
- 如何定制日历控件显示的星期文字
- Java集合源码学习(4)HashSet
- ClassLoader背景知识
- Normalizing Flows Tutorial(标准化流教程)第一部分
- cognos report上钻下钻报表处理方法(2)
- setCompoundDrawables和setCompoundDrawablesWithIntrinsicBounds区别
- α-β剪枝算法学习寄(蒟蒻向,巨佬勿入)
- Word导出PDF后,PDF没有生成Word中对应的目录
- 浅析Chrome Packaged Apps
- ESP32-cam使用-智能家居云端视频监控实现
- 无线路由器常用的五种工作模式详细介绍
- AndroidManifest.xml中常用属性及含义
- 机器人及无人驾驶领域真实人才供需状况如何?企业真正需要什么样的人才?
- 11家共享单车可通过支付宝免押骑车,这种省钱的方法你get了吗?
- 学好SEO需要掌握哪些知识要点?
热门文章
- linux curl获取头部信息,curl 命令如何获取 http header 返回的响应消息头
- php 修改文件的权限_php修改文件权限
- stm32for循环几个机械周期_带你了解包装机械设备的可调度性分析
- c语言字符串strl复制转换,C语言字符串基础学习
- java 默认为空的注解,错误注解的字段设置一个默认的空值
- kivy python 读取oracle数据库_python app (kivy)-与小型数据库连接,实现注册登录操作...
- JS实现键盘事件上下翻页
- alexnet的loss在一个epoch内没有稳定下降_深度学习中的双下降现象
- SIFT特征原理与理解
- MySQL主从数据同步延时分析