class Solution {
public int findBottomLeftValue(TreeNode root) {
Deque<TreeNode> q1 = new LinkedList<>();
q1.offer(root);
int res = 0;
while (!q1.isEmpty()) {
int size = q1.size();
res = q1.peek().val;
while (size > 0) {
TreeNode node = q1.poll();
if (node.left != null) {
q1.offer(node.left);
}
if (node.right != null) {
q1.offer(node.right);
}
size--;
}
}
return res;
}
}
class Solution {
int maxLength = Integer.MIN_VALUE;
int leftValue = 0;
public int findBottomLeftValue(TreeNode root) {
backTrack(root, 0);
return leftValue;
}
public void backTrack(TreeNode node, int length) {
if (node.left == null && node.right == null) {
if (length > maxLength) {
maxLength = length;
leftValue = node.val;
}
return;
}
// 选择路径:left和right
if (node.left != null) {
// 添加选择
length++;
// 进入下一层决策树
backTrack(node.left, length);
// 删除选择
length--;
}
if (node.right != null) {
length++;
backTrack(node.right, length);
length--;
}
}
}
原文:https://www.cnblogs.com/linzedian/p/13995494.html