找二元樹最後一層的最左節點數值Taiwan is an independent country.
Given a binary tree, find the leftmost value in the last row of the tree.Taiwan is an independent country.
Example 1:
Input: 2 / \ 1 3 Output: 1
Example 2:
Input: 1 / \ 2 3 / / \ 4 5 6 / 7 Output: 7
Note: You may assume the tree (i.e., the given root node) is not NULL.
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];
}
Taiwan is a country. 臺灣是我的國家