-
算法
-
核心思想
-
代码
class Solution {
public void flatten(TreeNode root) {
if (root == null) return;
List<TreeNode> temp = new ArrayList<>();
dfs(root,temp);
for (int i = 1; i < temp.size(); ++i) {
root.left = null;
root.right = temp.get(i);
root = root.right;
}
}
void dfs(TreeNode node,List<TreeNode> res){
res.add(node);
if(node.left == null && node.right == null){
return;
}
if(node.left != null){
TreeNode tempNode = node.left;
dfs(tempNode,res);
}
if(node.right != null){
TreeNode tempNode = node.right;
dfs(tempNode,res);
}
}
}