513. Find Bottom Left Tree Value
Solution
Do the degree traversal and find record the first one.
class Solution {
public int findBottomLeftValue(TreeNode root) {
int ret = root.val;
Queue<TreeNode> childs = new LinkedList<>();
childs.add(root);
while(!childs.isEmpty()){
int count = childs.size();
boolean isFirst = true;
while(count != 0){
TreeNode node = childs.poll();
if(isFirst){
ret = node.val;
isFirst = false;
}
if(node.left != null){
childs.add(node.left);
}
if(node.right != null){
childs.add(node.right);
}
count--;
}
}
return ret;
}
}
Last updated
Was this helpful?