513-find-bottom-left-tree-value

DevGod
Vtuber
/** * Definition for a binary tree node. * function TreeNode(val, left, right) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } *//** * @param {TreeNode} root * @return {number} */var findBottomLeftValue = function(root, L=0) { let ans = [-Infinity,-Infinity]; function travel(root,L=0){ if(ans[1] < L){ans = [root.val,L];} if(root.left != null){travel(root.left, L+1);} if(root.right != null){travel(root.right, L+1);} } travel(root); return ans[0];};
# 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 = rightclass Solution: def findBottomLeftValue(self, root: Optional[TreeNode]) -> int: if not root: return None
ans = [0,0] while arr: node = arr.pop() if(node.right): arr.append(node.right) if(node.left): arr.append(node.left)
print(node.val)
return ans[1]