513. Find Bottom Left Tree Value
- Find Bottom Left Tree Value python solution
题目描述
Given a binary tree, find the leftmost value in the last row of the tree.

解析
持续递归,就是速度有点慢。
// An highlighted block
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def findBottomLeftValue(self, root: TreeNode) -> int:
queue = [root]
for node in queue:
if node.right:
queue += [node.right]
if node.left:
queue += [node.left]
return node.val
Reference
https://leetcode.com/problems/find-bottom-left-tree-value/discuss/98779/Right-to-Left-BFS-(Python-%2B-Java)
本文介绍了一种使用Python实现的算法,旨在解决LeetCode上的问题:寻找二叉树最后一行最左边的节点值。通过广度优先搜索(BFS),从右向左遍历树的节点,确保找到的最后一个节点即为所求。
646

被折叠的 条评论
为什么被折叠?



