🟡 剑指 Offer II 045. 二叉树最底层最左边的值
LeetCode 提示
题目难度 中等
原题链接 🔗 leetcode
#
题解1/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */class Solution { public int findBottomLeftValue(TreeNode root) { Deque<TreeNode> queue = new LinkedList<>(); queue.add(root); TreeNode leftMost = root, cur; int rowSize;
while (!queue.isEmpty()) { rowSize = queue.size(); leftMost = queue.peek();
for (int i=0; i<rowSize; i+=1) { cur = queue.poll(); if (cur.left != null) { queue.add(cur.left); } if (cur.right != null) { queue.add(cur.right); } } }
return leftMost.val; }}