Straightforward Java recursive solution


  • 1
    D

    public class Solution {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {

        List<List<Integer>> ret=new LinkedList<List<Integer>>();
        if(root==null){
            return ret;
        }
        int level=0;
        recursive(root,ret,level);
        return ret;
    
    }
    
    public void recursive(TreeNode root, List<List<Integer>> ret,int level){
        if(root==null){
            return;
        }
        if(level>=ret.size()){
            List<Integer> newlink=new LinkedList<Integer>();
            ret.add(0,newlink);
        }
        ret.get(ret.size()-level-1).add(root.val);
        recursive(root.left,ret,level+1);
        recursive(root.right,ret,level+1);
    }
    

    }


Log in to reply
 

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