recursive solution


  • 0
    Z

    my solution
    public void help(TreeNode root,List<List<Integer>> res,int level){
    if(root == null)
    return;
    if(res.size() < level)
    res.add(0,new ArrayList<Integer>());
    List<Integer> s = res.get(res.size()-level);
    s.add(root.val);
    help(root.left,res,level+1);
    help(root.right,res,level+1);
    }
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
    List<List<Integer>> res = new ArrayList<List<Integer>>();
    help(root,res,1);
    return res;
    }


Log in to reply
 

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