I get TLE, but I wonder if my code works. Can somebody help me?


  • 0
    K
    public class Solution 
    {
        public List<Integer> inorderTraversal(TreeNode root) 
        {
            List<Integer> rt=new ArrayList<Integer>();
            if(root!=null)
            {
                Stack<TreeNode> st=new Stack<TreeNode>();
                TreeNode ptr=root;
                do
                {
                    if(ptr.right!=null)
                        st.push(ptr.right);
                    if(ptr.left==null)
                    {
                        rt.add(ptr.val);
                        if(!st.empty())
                            rt.add(st.pop().val);
                        if(!st.empty())    
                            ptr=st.pop();
                    }
                    else
                    {
                        st.push(ptr);
                        ptr=ptr.left;
                    }
                    
                }while(!st.empty());
            }
            return rt;
        }
    }
    

    I'm using public computer on which Java environment cannot be installed, so it is not me being too lazy to write my own test... It is just dozen line. I hope somebody can tell me on where my algorithm goes wrong.

    Very Very Grateful for your help :)


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.