博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode#114]Flatten Binary Tree to Linked List
阅读量:4339 次
发布时间:2019-06-07

本文共 3089 字,大约阅读时间需要 10 分钟。

The problem:

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

A wrong solution :

public class Solution {    public void flatten(TreeNode root) {        if (root == null)            return;            ArrayList
pre = new ArrayList
(); TreeNode dummy = new TreeNode(0); //use a dummy node for wirtting invariant pre.add(dummy); helper(root, pre); } private void helper(TreeNode cur, ArrayList
pre) { if (cur == null) return; TreeNode pre_node = pre.get(0); pre_node.left = null; pre_node.right = cur; pre.set(0, cur); helper(cur.left, pre);//this could change the right child value to cur, before reaching the next nextstatement. helper(cur.right, pre); }}

 

An analyssis for the error:

This question could be easily solved by using recursion, but when writing the program, we might introduce a big pitfall.Let's firstly consdiering the following buggy code:    private void helper(TreeNode cur, ArrayList
pre) { if (cur == null) return; TreeNode pre_node = pre.get(0); pre_node.left = null; pre_node.right = cur; pre.set(0, cur); helper(cur.left, pre); helper(cur.right, pre); } The buggy code looks extraordinarily fine, if we thinking it from the perspective of non-recursion program.A big pitfall:1 helper(cur.left, pre); //we call the recursive function2 helper(cur.right, pre);the statement 1 would change the value of cur node.(even it does not change current pointer)in : 1. pre_node.left = null;2. pre_node.right = cur;This would lead to a infinite loop, thus result in stack overflow.The classic way to solve this problem is to keep a copy of left child and right child's pointer copy. TreeNode left = cur.left; TreeNode right = cur.right;Thus any recursion in the lower level would not affect current level's value.(it's very important!!!)

My accepted solution:

public class Solution {    public void flatten(TreeNode root) {        if (root == null)            return;            ArrayList
pre = new ArrayList
(); TreeNode dummy = new TreeNode(0); //use a dummy node for wirtting invariant pre.add(dummy); helper(root, pre); } private void helper(TreeNode cur, ArrayList
pre) { if (cur == null) return; TreeNode pre_node = pre.get(0); pre_node.left = null; pre_node.right = cur; TreeNode left = cur.left; TreeNode right = cur.right; pre.set(0, cur); helper(left, pre); helper(right, pre); }}

 

转载于:https://www.cnblogs.com/airwindow/p/4217602.html

你可能感兴趣的文章
Jenkins安装配置
查看>>
个人工作总结05(第二阶段)
查看>>
Java clone() 浅拷贝 深拷贝
查看>>
深入理解Java虚拟机&运行时数据区
查看>>
02-环境搭建
查看>>
spring第二冲刺阶段第七天
查看>>
搜索框键盘抬起事件2
查看>>
阿里百川SDK初始化失败 错误码是203
查看>>
透析Java本质-谁创建了对象,this是什么
查看>>
BFS和DFS的java实现
查看>>
关于jquery中prev()和next()的用法
查看>>
一、 kettle开发、上线常见问题以及防错规范步骤
查看>>
eclipse没有server选项
查看>>
CRC码计算及校验原理的最通俗诠释
查看>>
使用Gitbook来编写你的Api文档
查看>>
jquery扩展 $.fn
查看>>
Markdown指南
查看>>
influxDB的安装和简单使用
查看>>
JPA框架学习
查看>>
JPA、JTA、XA相关索引
查看>>