文章目录
104. Maximum Depth of Binary Tree
- 递归法,后序遍历
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
def recur(node):
if not node:
return 0
h_left = recur(node.left)
h_right = recur(node.right)
return max(h_left,h_right)+1
return recur(root)
559. Maximum Depth of N-ary Tree
Given a n-ary tree, find its maximum depth.
Sol 1:
class Solution:
def maxDepth(self, root: 'Node') -> int:
if not root:
return 0
max_h=1
for child in root.children:
max_h = max(self.maxDepth(child)+1,max_h)
return max_h
Sol 2:
#bfs
class Solution1:
def maxDepth(self, root: 'Node') -> int:
if not root:
return 0
queue = deque([root])
res = 0
while queue:
res+=1
size = len(queue)
for _ in range(size):
node = queue.popleft()
for x in node.children:
if x:
queue.append(x)
return res
111. Minimum Depth of Binary Tree
Sol 1: 递归
class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
def min_recur(root):
if not root:
return 0
h_left = min_recur(root.left)
h_right = min_recur(root.right)
if not root.left and not root.right:
h = 1
elif not root.left and root.right:
h = h_right+1
elif not root.right and root.left:
h = h_left+1
else:
h = min(h_left, h_right)+1
return h
return min_recur(root)
Sol 2: BFS
class Solution1:
def minDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
queue = deque([root])
depth = 1
while queue:
size = len(queue)
for _ in range(size):
node = queue.popleft()
if not node.left and not node.right:
return depth
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
depth+=1
return depth
222. Count Complete Tree Nodes
Given the
root
of a complete binary tree, return the number of the nodes in the tree.
According to Wikipedia, every level, except possibly the last, is completely filled in a complete binary tree, and all nodes in the last level are as far left as possible. It can have between 1
and 2h
nodes inclusive at the last level h
.
Design an algorithm that runs in less than O(n)
time complexity.
Example 1:
![img](https://assets.leetcode.com/uploads/2021/01/14/complete.jpg)
Input: root = [1,2,3,4,5,6]
Output: 6
Sol 1: Complete tree formula
# Complete Tree
class Solution:
def countNodes(self, root: Optional[TreeNode]) -> int:
def count_recur(node)-> int:
if not node:
return 0
left, right = node.left, node.right
d_l, d_r = 0, 0
while left:
d_l+=1
left = left.left
while right:
d_r+=1
right = right.right
if d_l == d_r:
return (2<<d_l) - 1
left_num = count_recur(node.left)
right_num = count_recur(node.right)
return left_num + right_num + 1
return count_recur(root)
Sol 2: dfs recur
# Recur
class Solution2:
def countNodes(self, root: Optional[TreeNode]) -> int:
def count_recur(node) -> int:
if not node:
return 0
left = count_recur(node.left)
right = count_recur(node.right)
return left+right+1
return count_recur(root)
Sol3: BFS
class Solution1:
def countNodes(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
queue = deque([root])
res = 0
while queue:
size = len(queue)
level = 0
for _ in range(size):
node = queue.popleft()
level+=1
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
res+=level
return res