Binary Tree Inorder Traversal

题目
Given a binary tree, return the inorder traversal of its nodes' values.

For example:
Given binary tree [1,null,2,3],

   1
    \
     2
    /
   3

return [1,3,2].

Note: Recursive solution is trivial, could you do it iteratively?

答案
Recursive

class Solution {
    public List inorderTraversal(TreeNode root) {
        List list = new ArrayList();
        inorderTraversal(list, root);
        return list;
    }
    public void inorderTraversal(List list, TreeNode root) {
        if(root == null) return;
        inorderTraversal(list, root.left);
        list.add(root.val);
        inorderTraversal(list, root.right);
    }
}

Iterative

你可能感兴趣的:(Binary Tree Inorder Traversal)