My solution in java


  • 0
    M
    public class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<Integer>();
    	if(root != null){
    		if(root.left != null){
    			list.addAll(inorderTraversal(root.left));
    			list.add(root.val);
    			if(root.right != null){
    				list.addAll(inorderTraversal(root.right));
    			}
    		}else {
    			list.add(root.val);
    			if(root.right != null){
    				list.addAll(inorderTraversal(root.right));
    			}
    		}
    	}
    	return list;
    }
    

    }


  • 0
    W

    Try giving indentation to code using Ctrl+k (after selecting code). That way, it is more readable. It seems you are using recursion, since you are calling the same function to get all the elements on left side of the root, and also the same for right side. The question asks to do it iteratively.


Log in to reply
 

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