Open youngyangyang04 opened 5 months ago
class Solution {
public int findBottomLeftValue(TreeNode root) {
Queue<TreeNode> q = new LinkedList<>();
if (root != null) q.add(root);
int result = 0;
while (!q.isEmpty()) {
int size = q.size();
for (int i = 0; i < size; i++) {
TreeNode node = q.poll();
if (i == 0) result = node.val; // 1->2->4->7
if (node.left != null) q.add(node.left);
if (node.right != null) q.add(node.right);
}
}
return result;
}
}
https://www.programmercarl.com/0513.%E6%89%BE%E6%A0%91%E5%B7%A6%E4%B8%8B%E8%A7%92%E7%9A%84%E5%80%BC.html