tags: python Binary tree
107. Level of the binary tree traversal II
A bifurcous tree is given back to the hierarchy of its node value from the bottom. (Ie, press the layer where the leaf node is located to the root node, from left to right from left to right)
Is this two days really weekend, a daily question of the barracks is simple.
This question is a BFS, the best time complexity is n, if you want to optimize, Python can use Deque, improve the speed when inserting and deleting (if the tree is relatively large), but the measurement speed is not much improved.
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def levelOrderBottom(self, root: TreeNode) -> List[List[int]]:
ans = []
bfs = collections.deque([root])
if not root:
return ans
while(bfs):
stand = []
for _ in range(len(bfs)):
node = bfs.popleft()
stand.append(node.val)
if node.left:
bfs.append(node.left)
if node.right:
bfs.append(node.right)
ans.insert(0,stand)
return ans
link:binary-tree-level-order-traversal-ii Title description: Netizens contributed basically the same idea of solving the problem as mine, but because they don’t understand the container class,...
A bifurcous tree is given back to the hierarchy of its node value from the bottom. (Ie, press the layer where the leaf node is located to the root node, from left to right from left to right) E.g: Giv...
1. Editor I am using win10+vscode+leetcode+python3 See my blog for environment configuration: link 2. Question 107 (1) Title English: Given a binary tree, return the bottom-up level order traversal of...
LeetCode-107, Level Traversal of Binary Tree II-Simple Given a binary tree, return its node value to traverse from the bottom up. (That is, from the layer where the leaf node is located to the layer w...
A bifurcous tree is given back to the hierarchy of its node value from the bottom. (Ie, press the layer where the leaf node is located to the root node, from left to right from left to right) E.g: Giv...
link https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/ time consuming Solution: 20 min Question: 2 min Meaning A bifurcous tree is given back to the hierarchy of its node value fr...
111. Minimum depth of the binary tree gives a binary tree to find its minimum depth. The minimum depth is the number of nodes on the shortest path from the root node to the shortest path of the neares...
LeetCode 107. Binary tree level traversal II topic Given a binary tree, return the traversal of its node values from bottom to top. (That is, from the layer where the leaf node is located to the lay...