[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/mJackie/leetcode/master/code/lc144.java [Back]  [Original]

package code;
/*
 * 144. Binary Tree Preorder Traversal
 * 
 * Medium
 * Stack, Tree
 * 
 * Tipslc94,lc145, lc102
 */
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class lc144 {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }
    public List inorderTraversal(TreeNode root) {
        List res = new ArrayList();
        if(root==null)
            return res;
        Stack st = new Stack();
        while( !st.isEmpty() || root!=null ) {      //
            while (root != null) {
                st.push(root);
                res.add(root.val);
                root = root.left;
            }
            root = st.pop();
            root = root.right;
        }
        return res;
    }
}

Web Proxy Viewer  |  New URL  |  Original Page