65.找树左下角的值

时间:2024-10-21 13:25:31

class Solution {
    public int findBottomLeftValue(TreeNode root) {
        int res=0;
        if(root==null){
            return res;
        }
        TreeNode tn=root;
        Queue<TreeNode> qu=new LinkedList<>();
        qu.offer(tn);
        while(!qu.isEmpty()){
            int le=qu.size();
            for(int i=0;i<le;i++){
                tn=qu.poll();
                if(tn.left!=null){
                    qu.offer(tn.left);
                }
                if(tn.right!=null){
                    qu.offer(tn.right);
                }
                if(i==0){
                    res=tn.val;
                }
            }
        }
        return res;
    }
}
class Solution(object):
    def findBottomLeftValue(self, root):
        res=0
        if root is None:
            return res
        tn=root
        qu=collections.deque()
        qu.append(tn)
        while qu:
            le=len(qu)
            for i in range(le):
                tn=qu.popleft()
                if tn.left:
                    qu.append(tn.left)
                if tn.right:
                    qu.append(tn.right)
                if i==0:
                    res=tn.val
        return res

相关文章