mirror of
https://github.com/prdlk/leetcode.git
synced 2026-11-03 05:27:36 +00:00
Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
90082518a6 | ||
|
|
e10a913546 | ||
|
|
fec94ed636 | ||
|
|
30273d0030 | ||
|
|
50299defef | ||
|
|
c843be855e |
No files matched your search
@@ -0,0 +1,39 @@
|
||||
---
|
||||
title: '104. Maximum Depth of Binary Tree'
|
||||
description: Given the root of a binary tree, return its maximum depth
|
||||
sidebar:
|
||||
label: 'Maximum Depth of Binary Tree'
|
||||
badge: 'Easy'
|
||||
---
|
||||
|
||||
<Badge variant="accent">Tree DFS</Badge>
|
||||
|
||||
### Example 1:
|
||||
- Input: `root = [3,9,20,null,null,15,7]`
|
||||
- Output: `3`
|
||||
|
||||
### Example 2:
|
||||
- Input: `root = [1,null,2]`
|
||||
- Output: `2`
|
||||
|
||||
### Constraints:
|
||||
|
||||
- The number of nodes in the tree is in the range [0, 10^4].
|
||||
- `-100 <= Node.val <= 100`
|
||||
|
||||
## Solution
|
||||
|
||||
```py
|
||||
# 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 maxDepth(self, root: Optional[TreeNode]) -> int:
|
||||
if not root:
|
||||
return 0
|
||||
|
||||
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
|
||||
```
|
||||
@@ -0,0 +1,49 @@
|
||||
---
|
||||
title: '226. Invert Binary Tree'
|
||||
description: Given the root of a binary tree, invert the tree, and return its root
|
||||
sidebar:
|
||||
label: 'Invert Binary Tree'
|
||||
badge: 'Easy'
|
||||
---
|
||||
|
||||
<Badge variant="accent">Tree DFS</Badge>
|
||||
|
||||
### Example 1:
|
||||
- Input: `root = [4,2,7,1,3,6,9]`
|
||||
- Output: `[4,7,2,9,6,3,1]`
|
||||
|
||||
### Example 2:
|
||||
- Input: `root = [2,1,3]`
|
||||
- Output: `[2,3,1]`
|
||||
|
||||
### Example 3:
|
||||
- Input: `root = []`
|
||||
- Output: `[]`
|
||||
|
||||
### Constraints:
|
||||
|
||||
- The number of nodes in the tree is in the range [0, 100].
|
||||
- `-100 <= Node.val <= 100`
|
||||
|
||||
## Solution
|
||||
|
||||
```py
|
||||
# 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 invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
|
||||
if not root:
|
||||
return None
|
||||
|
||||
left = self.invertTree(root.left)
|
||||
right = self.invertTree(root.right)
|
||||
|
||||
root.left = right
|
||||
root.right = left
|
||||
|
||||
return root
|
||||
```
|
||||
@@ -0,0 +1,62 @@
|
||||
---
|
||||
title: '236. Lowest Common Ancestor of a Binary Tree'
|
||||
description: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree
|
||||
sidebar:
|
||||
label: 'Lowest Common Ancestor of a Binary Tree'
|
||||
badge: 'Medium'
|
||||
---
|
||||
|
||||
<Badge variant="accent">Tree DFS</Badge>
|
||||
|
||||
### Example 1:
|
||||
- Input: `root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1`
|
||||
- Output: `3`
|
||||
- Explanation: The LCA of nodes `5` and `1` is `3`.
|
||||
|
||||
### Example 2:
|
||||
- Input: `root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4`
|
||||
- Output: `5`
|
||||
- Explanation: The LCA of nodes `5` and `4` is `5`, since a node can be a descendant of itself according to the LCA definition.
|
||||
|
||||
### Example 3:
|
||||
- Input: `root = [1,2], p = 1, q = 2`
|
||||
- Output: `1`
|
||||
|
||||
### Constraints:
|
||||
|
||||
- The number of nodes in the tree is in the range [2, 10^5].
|
||||
- `-10^9 <= Node.val <= 10^9`
|
||||
- All Node.val are unique.
|
||||
- `p != q`
|
||||
- p and q will exist in the tree.
|
||||
|
||||
## Solution
|
||||
|
||||
```py
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, x):
|
||||
# self.val = x
|
||||
# self.left = None
|
||||
# self.right = None
|
||||
|
||||
|
||||
class Solution:
|
||||
def lowestCommonAncestor(
|
||||
self, root: "TreeNode", p: "TreeNode", q: "TreeNode"
|
||||
) -> "TreeNode":
|
||||
|
||||
if not root:
|
||||
return None
|
||||
|
||||
if root == p or root == q:
|
||||
return root
|
||||
|
||||
l = self.lowestCommonAncestor(root.left, p, q)
|
||||
r = self.lowestCommonAncestor(root.right, p, q)
|
||||
|
||||
if l and r:
|
||||
return root
|
||||
else:
|
||||
return l or r
|
||||
```
|
||||
@@ -0,0 +1,46 @@
|
||||
"""
|
||||
104. Maximum Depth of Binary Tree
|
||||
Difficulty: Easy
|
||||
https://leetcode.com/problems/maximum-depth-of-binary-tree/
|
||||
|
||||
──────────────────────────────────────────────────
|
||||
|
||||
Given the root of a binary tree, return its maximum depth.
|
||||
|
||||
A binary tree's maximum depth is the number of nodes along the
|
||||
longest path from the root node down to the farthest leaf node.
|
||||
|
||||
|
||||
|
||||
Example 1:
|
||||
|
||||
Input: root = [3,9,20,null,null,15,7]
|
||||
Output: 3
|
||||
|
||||
Example 2:
|
||||
|
||||
Input: root = [1,null,2]
|
||||
Output: 2
|
||||
|
||||
|
||||
|
||||
Constraints:
|
||||
|
||||
• The number of nodes in the tree is in the range [0, 10^4].
|
||||
|
||||
• -100 <= Node.val <= 100
|
||||
"""
|
||||
|
||||
|
||||
# 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 maxDepth(self, root: Optional[TreeNode]) -> int:
|
||||
if not root:
|
||||
return 0
|
||||
|
||||
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
|
||||
@@ -0,0 +1,54 @@
|
||||
"""
|
||||
226. Invert Binary Tree
|
||||
Difficulty: Easy
|
||||
https://leetcode.com/problems/invert-binary-tree/
|
||||
|
||||
──────────────────────────────────────────────────
|
||||
|
||||
Given the root of a binary tree, invert the tree, and return its root.
|
||||
|
||||
|
||||
|
||||
Example 1:
|
||||
|
||||
Input: root = [4,2,7,1,3,6,9]
|
||||
Output: [4,7,2,9,6,3,1]
|
||||
|
||||
Example 2:
|
||||
|
||||
Input: root = [2,1,3]
|
||||
Output: [2,3,1]
|
||||
|
||||
Example 3:
|
||||
|
||||
Input: root = []
|
||||
Output: []
|
||||
|
||||
|
||||
|
||||
Constraints:
|
||||
|
||||
• The number of nodes in the tree is in the range [0, 100].
|
||||
|
||||
• -100 <= Node.val <= 100
|
||||
"""
|
||||
|
||||
|
||||
# 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 invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
|
||||
if not root:
|
||||
return None
|
||||
|
||||
left = self.invertTree(root.left)
|
||||
right = self.invertTree(root.right)
|
||||
|
||||
root.left = right
|
||||
root.right = left
|
||||
|
||||
return root
|
||||
@@ -0,0 +1,76 @@
|
||||
"""
|
||||
236. Lowest Common Ancestor of a Binary Tree
|
||||
Difficulty: Medium
|
||||
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/
|
||||
|
||||
──────────────────────────────────────────────────
|
||||
|
||||
Given a binary tree, find the lowest common ancestor (LCA) of two
|
||||
given nodes in the tree.
|
||||
|
||||
According to the definition of LCA on Wikipedia: “The lowest
|
||||
common ancestor is defined between two nodes p and q as the lowest
|
||||
node in T that has both p and q as descendants (where we allow a node
|
||||
to be a descendant of itself).”
|
||||
|
||||
|
||||
|
||||
Example 1:
|
||||
|
||||
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
|
||||
Output: 3
|
||||
Explanation: The LCA of nodes 5 and 1 is 3.
|
||||
|
||||
Example 2:
|
||||
|
||||
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
|
||||
Output: 5
|
||||
Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a
|
||||
descendant of itself according to the LCA definition.
|
||||
|
||||
Example 3:
|
||||
|
||||
Input: root = [1,2], p = 1, q = 2
|
||||
Output: 1
|
||||
|
||||
|
||||
|
||||
Constraints:
|
||||
|
||||
• The number of nodes in the tree is in the range [2, 10^5].
|
||||
|
||||
• -10^9 <= Node.val <= 10^9
|
||||
|
||||
• All Node.val are unique.
|
||||
|
||||
• p != q
|
||||
|
||||
• p and q will exist in the tree.
|
||||
"""
|
||||
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, x):
|
||||
# self.val = x
|
||||
# self.left = None
|
||||
# self.right = None
|
||||
|
||||
|
||||
class Solution:
|
||||
def lowestCommonAncestor(
|
||||
self, root: "TreeNode", p: "TreeNode", q: "TreeNode"
|
||||
) -> "TreeNode":
|
||||
|
||||
if not root:
|
||||
return None
|
||||
|
||||
if root == p or root == q:
|
||||
return root
|
||||
|
||||
l = self.lowestCommonAncestor(root.left, p, q)
|
||||
r = self.lowestCommonAncestor(root.right, p, q)
|
||||
|
||||
if l and r:
|
||||
return root
|
||||
else:
|
||||
return l or r
|
||||
Reference in new issue
Block a user