R4-树与二叉树
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
if not root.left and not root.right:
return root.val
ret1=root.val
num1=1
node=root
while node.left:
ret1=node.left.val
node=node.left
num1+=1
if not root.right:
return ret1
else:
node=root.right
ret2=node.val
num2=2
while node.left:
ret2=node.left.val
node=node.left
num2+=1
if num1>=num2:
return ret1
else:
return ret2
递归写法
队列
无敌了
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
q=deque([root])
while q:
node=q.popleft()
if node.right:
q.append(node.right)
if node.left:
q.append(node.left)
return node.val
ps:
今天很sad