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

也就是说用先序遍历的方式将所有的节点都放到右侧来,我这里的方法的主要思想就是每次左侧存在节点的时候就将其原封不动的搬移到右侧来,代码如下:

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void flatten(TreeNode* root) {
while(root){
if(root->left){
TreeNode * leftBegin = root->left;
root->left = NULL;
TreeNode * leftEnd = leftBegin;
while(leftEnd->right)
leftEnd = leftEnd->right;
TreeNode * tmpRight = root->right;
root->right = leftBegin;
leftEnd->right = tmpRight;
}
root = root->right;
}
}
};

用java写了一遍,方法与上面基本相同,代码如下:

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public void flatten(TreeNode root) {
while(root != null){
if(root.left != null){
TreeNode tmp = root.right;
root.right = root.left;
TreeNode tmpRight = root.right;
while(tmpRight.right != null)
tmpRight = tmpRight.right;
tmpRight.right = tmp;
root.left = null;
}
root = root.right;
}
}
}

LeetCode OJ:Flatten Binary Tree to Linked List(捋平二叉树)的更多相关文章

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

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

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

  3. leetcode dfs Flatten Binary Tree to Linked List

    Flatten Binary Tree to Linked List Total Accepted: 25034 Total Submissions: 88947My Submissions Give ...

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

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

  6. [Leetcode][JAVA] 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   ...

  7. 【leetcode】Flatten Binary Tree to Linked List (middle)

    Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...

  8. leetcode 114 Flatten Binary Tree to Linked List ----- java

    Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 T ...

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

  10. [LeetCode] 114. Flatten Binary Tree to Linked List_Medium tag: DFS

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

随机推荐

  1. Hexo+yilia添加helper-live2d插件宠物动画,很好玩的哦~~

    个人主页:https://www.yuehan.online 现在博客:www.wangyurui.top 安装模块: 博客根目录选择cmd命令窗口或者git bash 输入以下代码,安装插件 操作: ...

  2. 005-快捷键,host,查看版本

    一.系统快捷键 设置 键盘--查看 shift+ctrl +print  区域截图至剪切版 ctrl+alt+箭头     切换工作区 自定义打开终端快捷键 设置->键盘->自定义:名称: ...

  3. Scrapy框架2

    一.进程.线程.协成 1.进程.线程.协成之间的关系          1.  线程是计算机中最小的工作单元.              2. 进程是提供资源供n个线程使用,即进程是最小的管理单元. ...

  4. uuid的使用

    1.mysql中直接使用uuid()函数,可以生成一个随机的uuid 正常的uuid是36位长度的,其中有4个字符是‘-’,在mysql中可以使用replace()函数来替换‘-’ insert in ...

  5. jQuery:自学笔记(2)——jQuery选择器

    jQuery:自学笔记(2)——jQuery选择器 基本选择器 说明 jQuery的基本选择器与CSS的选择器相似: 实例 标签选择器 //使用标签选择器更改字体大小 $(div).css('font ...

  6. Ajax在jQuery中的应用---ajax()方法

    在jQuery中,$.ajax()方法是最底层的方法,也是功能最强的方法.其调用的语法格式为: $.ajax([options]) 其中,可选项参数[options]为$.ajax()方法中的请求设置 ...

  7. PHP字符串函数大全

    无论哪种编程语言,字符串操作都是一个重要的基础,往往简单而重要.PHP为我们提供了大量的字符串操作函数,功能强大,使用也比较简单.在这里结合实例总结分析PHP字符串函数的功能. 1.addcslash ...

  8. Xib与Storyboard相关知识点

    相同点 都用来描述软件界面 都用Interface Builder工具来编辑 本质都是转换成代码去创建控件 不同点 Xib是轻量级的,用来描述局部的UI界面 Storyboard是重量级的,用来描述整 ...

  9. Python 字符串概念和操作

    # 字符串概念:由单个字符串组成的一个集合 # 普通字符串(非原始字符串) str = "abc" print(str) # abc # 原始字符串(前面加r) str = r&q ...

  10. JAVA 使用qq邮箱发送邮件

    引入一个架包: gradle( "com.sun.mail:javax.mail:1.5.6", ) 代码如下: private static final String QQ_EM ...