|
| 1 | +Sure! Here's the full LeetCode-style `README.md` file for **236. Lowest Common Ancestor of a Binary Tree**, formatted just like in the `doocs/leetcode` GitHub repository. |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +```markdown |
| 6 | +--- |
| 7 | +title: "236. Lowest Common Ancestor of a Binary Tree" |
| 8 | +description: "Recursive DFS solution to find the lowest common ancestor of two nodes in a binary tree." |
| 9 | +difficulty: Medium |
| 10 | +tags: |
| 11 | + - Tree |
| 12 | + - Depth-First Search |
| 13 | + - Binary Tree |
| 14 | +--- |
| 15 | + |
| 16 | +## Description |
| 17 | + |
| 18 | +Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. |
| 19 | + |
| 20 | +According to the [definition of LCA on Wikipedia](https://en.wikipedia.org/wiki/Lowest_common_ancestor): |
| 21 | +“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).” |
| 22 | + |
| 23 | +### Example 1: |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +``` |
| 28 | +Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 |
| 29 | +Output: 3 |
| 30 | +Explanation: The LCA of nodes 5 and 1 is 3. |
| 31 | +``` |
| 32 | +
|
| 33 | +### Example 2: |
| 34 | +
|
| 35 | +``` |
| 36 | +Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4 |
| 37 | +Output: 5 |
| 38 | +Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself. |
| 39 | +``` |
| 40 | +
|
| 41 | +### Example 3: |
| 42 | +
|
| 43 | +``` |
| 44 | +Input: root = [1,2], p = 1, q = 2 |
| 45 | +Output: 1 |
| 46 | +``` |
| 47 | +
|
| 48 | +### Constraints: |
| 49 | +
|
| 50 | +- The number of nodes in the tree is in the range [2, 10⁵]. |
| 51 | +- -10⁹ <= Node.val <= 10⁹ |
| 52 | +- All Node.val are **unique**. |
| 53 | +- `p != q` |
| 54 | +- `p` and `q` will exist in the tree. |
| 55 | +
|
| 56 | +--- |
| 57 | +
|
| 58 | +## Solutions |
| 59 | +
|
| 60 | +### Approach: Recursion (DFS) |
| 61 | +
|
| 62 | +We recursively search the tree: |
| 63 | +
|
| 64 | +- If the current node is `null`, or matches `p` or `q`, return it. |
| 65 | +- Recursively search the left and right subtrees. |
| 66 | +- If both sides return non-null, this node is the LCA. |
| 67 | +- Otherwise, return the non-null result from one of the sides. |
| 68 | +
|
| 69 | +**Time Complexity:** O(n) |
| 70 | +**Space Complexity:** O(n) |
| 71 | +
|
| 72 | +--- |
| 73 | +
|
| 74 | +### Code |
| 75 | +
|
| 76 | +#### Python3 |
| 77 | +
|
| 78 | +```python |
| 79 | +# Definition for a binary tree node. |
| 80 | +# class TreeNode: |
| 81 | +# def __init__(self, x): |
| 82 | +# self.val = x |
| 83 | +# self.left = None |
| 84 | +# self.right = None |
| 85 | +
|
| 86 | +class Solution: |
| 87 | + def lowestCommonAncestor( |
| 88 | + self, root: "TreeNode", p: "TreeNode", q: "TreeNode" |
| 89 | + ) -> "TreeNode": |
| 90 | + if root in (None, p, q): |
| 91 | + return root |
| 92 | + left = self.lowestCommonAncestor(root.left, p, q) |
| 93 | + right = self.lowestCommonAncestor(root.right, p, q) |
| 94 | + return root if left and right else (left or right) |
| 95 | +``` |
| 96 | + |
| 97 | +#### Java |
| 98 | + |
| 99 | +```java |
| 100 | +/** |
| 101 | + * Definition for a binary tree node. |
| 102 | + * public class TreeNode { |
| 103 | + * int val; |
| 104 | + * TreeNode left; |
| 105 | + * TreeNode right; |
| 106 | + * TreeNode(int x) { val = x; } |
| 107 | + * } |
| 108 | + */ |
| 109 | +class Solution { |
| 110 | + public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { |
| 111 | + if (root == null || root == p || root == q) { |
| 112 | + return root; |
| 113 | + } |
| 114 | + var left = lowestCommonAncestor(root.left, p, q); |
| 115 | + var right = lowestCommonAncestor(root.right, p, q); |
| 116 | + if (left != null && right != null) { |
| 117 | + return root; |
| 118 | + } |
| 119 | + return left == null ? right : left; |
| 120 | + } |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +#### C++ |
| 125 | + |
| 126 | +```cpp |
| 127 | +/** |
| 128 | + * Definition for a binary tree node. |
| 129 | + * struct TreeNode { |
| 130 | + * int val; |
| 131 | + * TreeNode *left; |
| 132 | + * TreeNode *right; |
| 133 | + * TreeNode(int x) : val(x), left(NULL), right(NULL) {} |
| 134 | + * }; |
| 135 | + */ |
| 136 | +class Solution { |
| 137 | +public: |
| 138 | + TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { |
| 139 | + if (root == nullptr || root == p || root == q) { |
| 140 | + return root; |
| 141 | + } |
| 142 | + auto left = lowestCommonAncestor(root->left, p, q); |
| 143 | + auto right = lowestCommonAncestor(root->right, p, q); |
| 144 | + if (left && right) { |
| 145 | + return root; |
| 146 | + } |
| 147 | + return left ? left : right; |
| 148 | + } |
| 149 | +}; |
| 150 | +``` |
| 151 | +
|
| 152 | +#### Go |
| 153 | +
|
| 154 | +```go |
| 155 | +func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode { |
| 156 | + if root == nil || root == p || root == q { |
| 157 | + return root |
| 158 | + } |
| 159 | + left := lowestCommonAncestor(root.Left, p, q) |
| 160 | + right := lowestCommonAncestor(root.Right, p, q) |
| 161 | + if left != nil && right != nil { |
| 162 | + return root |
| 163 | + } |
| 164 | + if left != nil { |
| 165 | + return left |
| 166 | + } |
| 167 | + return right |
| 168 | +} |
| 169 | +``` |
| 170 | + |
| 171 | +#### TypeScript |
| 172 | + |
| 173 | +```ts |
| 174 | +function lowestCommonAncestor( |
| 175 | + root: TreeNode | null, |
| 176 | + p: TreeNode | null, |
| 177 | + q: TreeNode | null, |
| 178 | +): TreeNode | null { |
| 179 | + if (!root || root === p || root === q) { |
| 180 | + return root; |
| 181 | + } |
| 182 | + const left = lowestCommonAncestor(root.left, p, q); |
| 183 | + const right = lowestCommonAncestor(root.right, p, q); |
| 184 | + return left && right ? root : left || right; |
| 185 | +} |
| 186 | +``` |
| 187 | + |
| 188 | +#### Rust |
| 189 | + |
| 190 | +```rust |
| 191 | +use std::cell::RefCell; |
| 192 | +use std::rc::Rc; |
| 193 | +impl Solution { |
| 194 | + pub fn lowest_common_ancestor( |
| 195 | + root: Option<Rc<RefCell<TreeNode>>>, |
| 196 | + p: Option<Rc<RefCell<TreeNode>>>, |
| 197 | + q: Option<Rc<RefCell<TreeNode>>>, |
| 198 | + ) -> Option<Rc<RefCell<TreeNode>>> { |
| 199 | + if root.is_none() || root == p || root == q { |
| 200 | + return root; |
| 201 | + } |
| 202 | + let left = Self::lowest_common_ancestor( |
| 203 | + root.as_ref().unwrap().borrow().left.clone(), |
| 204 | + p.clone(), |
| 205 | + q.clone(), |
| 206 | + ); |
| 207 | + let right = Self::lowest_common_ancestor( |
| 208 | + root.as_ref().unwrap().borrow().right.clone(), |
| 209 | + p.clone(), |
| 210 | + q.clone(), |
| 211 | + ); |
| 212 | + if left.is_some() && right.is_some() { |
| 213 | + return root; |
| 214 | + } |
| 215 | + if left.is_none() { |
| 216 | + return right; |
| 217 | + } |
| 218 | + return left; |
| 219 | + } |
| 220 | +} |
| 221 | +``` |
| 222 | + |
| 223 | +#### JavaScript |
| 224 | + |
| 225 | +```js |
| 226 | +var lowestCommonAncestor = function (root, p, q) { |
| 227 | + if (!root || root === p || root === q) { |
| 228 | + return root; |
| 229 | + } |
| 230 | + const left = lowestCommonAncestor(root.left, p, q); |
| 231 | + const right = lowestCommonAncestor(root.right, p, q); |
| 232 | + return left && right ? root : left || right; |
| 233 | +}; |
| 234 | +``` |
| 235 | + |
| 236 | +--- |
| 237 | +``` |
| 238 | +
|
| 239 | +Let me know if you’d like the Chinese version or a version for any other specific language or format! |
0 commit comments