# 一、题目描述

-100 <= Node.val <= 100

# 三、Java代码实现

/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode() {}
*     TreeNode(int val) { this.val = val; }
*     TreeNode(int val, TreeNode left, TreeNode right) {
*         this.val = val;
*         this.left = left;
*         this.right = right;
*     }
* }
*/
class Solution {
List<TreeNode> list = new ArrayList<>();
public void flatten(TreeNode root) {
dfs(root);
for(TreeNode node : list){
p.right = node;
p.left = null;
p = p.right;
}
}
void dfs(TreeNode node){
if(node==null){
return;
}
dfs(node.left);
dfs(node.right);
}
}

# 五、进阶

/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode() {}
*     TreeNode(int val) { this.val = val; }
*     TreeNode(int val, TreeNode left, TreeNode right) {
*         this.val = val;
*         this.left = left;
*         this.right = right;
*     }
* }
*/
class Solution {
public void flatten(TreeNode root) {
TreeNode p = root;
while(p!=null){
if(p.left!=null){
TreeNode next = p.left;
TreeNode pre = p.left;
while(pre.right!=null){
pre = pre.right;
}
pre.right = p.right;
p.right = next;
p.left = null;
}
p = p.right;
}
}
}

|
1天前
|

Java数据结构与算法：线性数据结构之链表
Java数据结构与算法：线性数据结构之链表
|
4天前
|

[Java·算法·简单] LeetCode 283. 移动零
[Java·算法·简单] LeetCode 283. 移动零
12 2
|
4天前
|

[Java·算法·中等] LeetCode21. 合并两个有序链表
[Java·算法·中等] LeetCode21. 合并两个有序链表
12 2
|
7天前
|

6 1
|
7天前

7 1
|
7天前

8 1
|
7天前
|
Java Python

10 1
|
11天前
|

11 1
|
11天前
|

13 2
|
11天前
|

LeetCode题目104: 二叉树的最大深度(递归\迭代\层序遍历\尾递归优化\分治法实现 )
LeetCode题目104: 二叉树的最大深度(递归\迭代\层序遍历\尾递归优化\分治法实现 )
5 0