package leetcode;

/**
 * https://leetcode.com/problems/find-bottom-left-tree-value/
 */
public class Problem513 {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    public int findBottomLeftValue(TreeNode root) {
        // TODO
        return 0;
    }

    public static void main(String[] args) {
        Problem513 prob = new Problem513();

        TreeNode root = new TreeNode(2);
        root.left = new TreeNode(1);
        root.right = new TreeNode(3);
        System.out.println(prob.findBottomLeftValue(root)); // 1

        root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.left.left = new TreeNode(4);
        root.right = new TreeNode(3);
        root.right.left = new TreeNode(5);
        root.right.right = new TreeNode(6);
        root.right.left.left = new TreeNode(7);
        System.out.println(prob.findBottomLeftValue(root)); // 7
    }
}

@@ -12,16 +12,145 @@
 tcode;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.LinkedList;%0Aimport java.util.List;%0Aimport java.util.Map;%0A%0A
 /**%0A * h
@@ -465,169 +465,273 @@
     
-// TODO%0A        return 0
+LinkedList%3CTreeNodeLevel%3E queue = new LinkedList%3C%3E();%0A        Map%3CInteger, List%3CInteger%3E%3E map = new HashMap%3C%3E()
 ;%0A    
-%7D%0A%0A
     
-public static void main(String%5B%5D args) %7B%0A        Problem513 prob = new Problem513();%0A%0A        TreeNode root = new TreeNod
+queue.add(new TreeNodeLevel(root, 1));%0A        int max = 0;%0A        while (!queue.isEmpty()) %7B%0A            TreeNodeLevel current = queue.remov
 e(
-2
 );%0A 
@@ -741,207 +741,325 @@
     
-root.left = new TreeNode(1);%0A        root.right = new TreeNode(3);%0A        System.out.println(prob.findBottomLeftValue(root)); // 1%0A%0A        root = new TreeNode(1);%0A        root.left = new TreeNode(2
+    max = Math.max(max, current.level);%0A            if (!map.containsKey(current.level)) %7B%0A                List%3CInteger%3E list = new ArrayList%3C%3E();%0A                list.add(current.node.val);%0A                map.put(current.level, list);%0A            %7D else %7B%0A                map.get(current.level).add(current.node.val
 );%0A 
@@ -1069,109 +1069,379 @@
     
+  
-root.left
+  %7D%0A            if (current.node
 .left 
+!
 = n
-ew TreeNode(4);%0A        root
+ull) %7B%0A                queue.add(new TreeNodeLevel(current.node.left, current.level + 1));%0A            %7D%0A            if (current.node
 .right 
+!
 = n
-ew TreeNode(3);%0A        root.right.left = new
+ull) %7B%0A                queue.add(new TreeNodeLevel(current.node.right, current.level + 1));%0A            %7D%0A        %7D%0A        return map.get(max).get(0);%0A    %7D%0A%0A    private static class
  Tre
@@ -1449,12 +1449,15 @@
 Node
-(5);
+Level %7B
 %0A   
@@ -1465,30 +1465,21 @@
     
-root.right.right = new
+private final
  Tre
@@ -1487,11 +1487,13 @@
 Node
-(6)
+ node
 ;%0A  
@@ -1502,112 +1502,162 @@
     
-root.right.left.left = new TreeNode(7);%0A        System.out.println(prob.findBottomLeftValue(root)); // 7
+private final int level;%0A%0A        public TreeNodeLevel(TreeNode node, int level) %7B%0A            this.node = node;%0A            this.level = level;%0A        %7D
 %0A   

Solve problem 513