94. Binary Tree Inorder Traversal(inorder ) ***(to be continue)easy

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

Example:

Input: [1,null,2,3]
   1
    \
     2
    /
   3

Output: [1,3,2]

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

 Recursive solution

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    List<Integer> res = new ArrayList<Integer>();
    public List<Integer> inorderTraversal(TreeNode root) {
        //inorder traveral : left root right
        inorder( root);
        return res;
    }
    void inorder(TreeNode node){
        if(node==null) return;
        inorder(node.left);
        res.add(node.val);
        inorder(node.right);
        
    }
}

 

follow up questions

 

 

 

 

posted @ 2018-07-09 13:14  wz30  阅读(106)  评论(0编辑  收藏  举报