/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<Integer> inorderTraversal(TreeNode root)
{
List<Integer> answer = new ArrayList<Integer>();//这里用ArrayList或者LinkedList应该没有影响吧
//Line 13: error: '(' or '[' expected居然出现括号漏写的错误
Stack<TreeNode> storeroot = new Stack<TreeNode>();//仿照前序的方法。我用个stack把根存起来。
while(root != null)//不大确定这个条件对不对。必须要确定 root等于null的时候全部处理完
{
if(root.left != null) //左结点不为空的情况下,将根放入Stack,优先考虑往左结点走
{
storeroot.push(root);
root = root.left;
}
else //左结点为空的情况下,根节点优先,可将根节点存入答案。如果有右结点,并选择向右走
{
answer.add(root.val);//Line 25: error: no suitable method found for add(TreeNode)
//answer.add(root);这种错误也要少犯
if(root.right != null)
{
root = root.right;
}
else if(!storeroot.isEmpty())
{
root = storeroot.pop();//如果pop出null的结果,循环应该是可以handle的
//Runtime Error Message: Line 34: java.util.EmptyStackException
//Last executed input: {1} 看来handle不了,只能把else换成else if了
//Line 34: error: cannot find symbol: variable pop
//root = storeroot.pop;方法都是要带括号的,给自己跪了
root.left = null;
//没有加上面这个语句之前,会出现Time Limit Exceeded,Last executed input: {1,2}这样的错误
//因为每一次程序从storeroot.pop里面读出之前的根结点时,树的结构未变,
//程序还是会一直向左走,陷入无限循环,自然就超时了
}
else break;//找不到root就可以 break了吧
}
}
return answer;
}
}
Binary Tree Inorder Traversal
最新推荐文章于 2021-08-27 00:22:25 发布