Simply put values into lists and get first item from the last list (Java)


  • 0
    A
    public int findBottomLeftValue(TreeNode root) {
        final List<List<TreeNode>> lists = new ArrayList<List<TreeNode>>();
        fillLists(lists, root, 0);
        final List<TreeNode> lastList = lists.get(lists.size()-1);
        return lastList.get(0).val;
    }
    
    public void fillLists(List<List<TreeNode>> lists, TreeNode node, int level) {
        if(node==null) {
            return; 
        }
        List<TreeNode> list;            
        if(level > (lists.size()-1)) {
            list = new ArrayList<TreeNode>();
            lists.add(list);
        }
        else { 
            list = lists.get(level); 
        }
        list.add(node);
        fillLists(lists, node.left, level+1);
        fillLists(lists, node.right, level+1);
    }

Log in to reply
 

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