C# One function


  • 0
    L

    public int FindBottomLeftValue(TreeNode root, int level = 0, int[] lst = null)
    {//lst[0]:max level, lst[1]:leftmost val
    lst = lst ?? new int[] { level, root.val };
    if (root.left == null)
    {
    if (root.right == null)
    {
    if (lst == null)
    lst = new int[] { level, root.val };
    else if (level > lst[0])
    {
    lst[0] = level;
    lst[1] = root.val;
    }
    }
    }
    else
    FindBottomLeftValue(root.left, level + 1, lst);
    if (root.right != null)
    FindBottomLeftValue(root.right, level + 1, lst);
    return lst[1];
    }


Log in to reply
 

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