|
| 1 | +Here is the complete LeetCode-style `README.md` for **872. Leaf-Similar Trees**, following the format you've requested (similar to `doocs/leetcode`): |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +```markdown |
| 6 | +# 872. Leaf-Similar Trees |
| 7 | + |
| 8 | +> Difficulty: Easy |
| 9 | +> Tags: Tree, Depth-First Search, Binary Tree |
| 10 | +> Link: [LeetCode Problem](https://leetcode.com/problems/leaf-similar-trees) |
| 11 | + |
| 12 | +## Description |
| 13 | + |
| 14 | +Consider all the leaves of a binary tree. From left to right order, the values of those leaves form a **leaf value sequence**. |
| 15 | + |
| 16 | +Two binary trees are considered **leaf-similar** if their leaf value sequences are the same. |
| 17 | + |
| 18 | +Return `true` if and only if the two given trees with head nodes `root1` and `root2` are leaf-similar. |
| 19 | + |
| 20 | +### Example 1: |
| 21 | + |
| 22 | + |
| 23 | + |
| 24 | +``` |
| 25 | +Input: root1 = [3,5,1,6,2,9,8,null,null,7,4], root2 = [3,5,1,6,7,4,2,null,null,null,null,null,null,9,8] |
| 26 | +Output: true |
| 27 | +``` |
| 28 | +
|
| 29 | +### Example 2: |
| 30 | +
|
| 31 | + |
| 32 | +
|
| 33 | +``` |
| 34 | +Input: root1 = [1,2,3], root2 = [1,3,2] |
| 35 | +Output: false |
| 36 | +``` |
| 37 | +
|
| 38 | +## Constraints |
| 39 | +
|
| 40 | +- The number of nodes in each tree will be in the range `[1, 200]`. |
| 41 | +- Both of the given trees will have values in the range `[0, 200]`. |
| 42 | +
|
| 43 | +--- |
| 44 | +
|
| 45 | +## Solutions |
| 46 | +
|
| 47 | +### Approach 1: DFS |
| 48 | +
|
| 49 | +We can perform a depth-first search (DFS) to collect the leaf values of each tree. If the leaf value sequences of both trees are the same, then the trees are leaf-similar. |
| 50 | +
|
| 51 | +**Time Complexity:** O(n) |
| 52 | +**Space Complexity:** O(n) |
| 53 | +
|
| 54 | +--- |
| 55 | +
|
| 56 | +### Python3 |
| 57 | +
|
| 58 | +```python |
| 59 | +# Definition for a binary tree node. |
| 60 | +# class TreeNode: |
| 61 | +# def __init__(self, val=0, left=None, right=None): |
| 62 | +# self.val = val |
| 63 | +# self.left = left |
| 64 | +# self.right = right |
| 65 | +class Solution: |
| 66 | + def leafSimilar(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool: |
| 67 | + def dfs(root: Optional[TreeNode], nums: List[int]) -> None: |
| 68 | + if root.left == root.right: |
| 69 | + nums.append(root.val) |
| 70 | + return |
| 71 | + if root.left: |
| 72 | + dfs(root.left, nums) |
| 73 | + if root.right: |
| 74 | + dfs(root.right, nums) |
| 75 | +
|
| 76 | + l1, l2 = [], [] |
| 77 | + dfs(root1, l1) |
| 78 | + dfs(root2, l2) |
| 79 | + return l1 == l2 |
| 80 | +``` |
| 81 | + |
| 82 | +--- |
| 83 | + |
| 84 | +### Java |
| 85 | + |
| 86 | +```java |
| 87 | +class Solution { |
| 88 | + public boolean leafSimilar(TreeNode root1, TreeNode root2) { |
| 89 | + List<Integer> l1 = new ArrayList<>(); |
| 90 | + List<Integer> l2 = new ArrayList<>(); |
| 91 | + dfs(root1, l1); |
| 92 | + dfs(root2, l2); |
| 93 | + return l1.equals(l2); |
| 94 | + } |
| 95 | + |
| 96 | + private void dfs(TreeNode root, List<Integer> nums) { |
| 97 | + if (root.left == root.right) { |
| 98 | + nums.add(root.val); |
| 99 | + return; |
| 100 | + } |
| 101 | + if (root.left != null) dfs(root.left, nums); |
| 102 | + if (root.right != null) dfs(root.right, nums); |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +--- |
| 108 | + |
| 109 | +### C++ |
| 110 | + |
| 111 | +```cpp |
| 112 | +class Solution { |
| 113 | +public: |
| 114 | + bool leafSimilar(TreeNode* root1, TreeNode* root2) { |
| 115 | + vector<int> l1, l2; |
| 116 | + dfs(root1, l1); |
| 117 | + dfs(root2, l2); |
| 118 | + return l1 == l2; |
| 119 | + } |
| 120 | + |
| 121 | + void dfs(TreeNode* root, vector<int>& nums) { |
| 122 | + if (root->left == root->right) { |
| 123 | + nums.push_back(root->val); |
| 124 | + return; |
| 125 | + } |
| 126 | + if (root->left) dfs(root->left, nums); |
| 127 | + if (root->right) dfs(root->right, nums); |
| 128 | + } |
| 129 | +}; |
| 130 | +``` |
| 131 | + |
| 132 | +--- |
| 133 | + |
| 134 | +### Go |
| 135 | + |
| 136 | +```go |
| 137 | +func leafSimilar(root1 *TreeNode, root2 *TreeNode) bool { |
| 138 | + l1, l2 := []int{}, []int{} |
| 139 | + var dfs func(*TreeNode, *[]int) |
| 140 | + dfs = func(root *TreeNode, nums *[]int) { |
| 141 | + if root.Left == root.Right { |
| 142 | + *nums = append(*nums, root.Val) |
| 143 | + return |
| 144 | + } |
| 145 | + if root.Left != nil { |
| 146 | + dfs(root.Left, nums) |
| 147 | + } |
| 148 | + if root.Right != nil { |
| 149 | + dfs(root.Right, nums) |
| 150 | + } |
| 151 | + } |
| 152 | + dfs(root1, &l1) |
| 153 | + dfs(root2, &l2) |
| 154 | + return reflect.DeepEqual(l1, l2) |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +--- |
| 159 | + |
| 160 | +### Rust |
| 161 | + |
| 162 | +```rust |
| 163 | +use std::cell::RefCell; |
| 164 | +use std::rc::Rc; |
| 165 | + |
| 166 | +impl Solution { |
| 167 | + pub fn leaf_similar( |
| 168 | + root1: Option<Rc<RefCell<TreeNode>>>, |
| 169 | + root2: Option<Rc<RefCell<TreeNode>>>, |
| 170 | + ) -> bool { |
| 171 | + let mut l1 = Vec::new(); |
| 172 | + let mut l2 = Vec::new(); |
| 173 | + Self::dfs(&root1, &mut l1); |
| 174 | + Self::dfs(&root2, &mut l2); |
| 175 | + l1 == l2 |
| 176 | + } |
| 177 | + |
| 178 | + fn dfs(node: &Option<Rc<RefCell<TreeNode>>>, nums: &mut Vec<i32>) { |
| 179 | + if let Some(n) = node { |
| 180 | + let n = n.borrow(); |
| 181 | + if n.left.is_none() && n.right.is_none() { |
| 182 | + nums.push(n.val); |
| 183 | + return; |
| 184 | + } |
| 185 | + if n.left.is_some() { |
| 186 | + Self::dfs(&n.left, nums); |
| 187 | + } |
| 188 | + if n.right.is_some() { |
| 189 | + Self::dfs(&n.right, nums); |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | +} |
| 194 | +``` |
| 195 | + |
| 196 | +--- |
| 197 | + |
| 198 | +### JavaScript |
| 199 | + |
| 200 | +```javascript |
| 201 | +var leafSimilar = function (root1, root2) { |
| 202 | + const l1 = []; |
| 203 | + const l2 = []; |
| 204 | + const dfs = (root, nums) => { |
| 205 | + if (root.left === root.right) { |
| 206 | + nums.push(root.val); |
| 207 | + return; |
| 208 | + } |
| 209 | + if (root.left) dfs(root.left, nums); |
| 210 | + if (root.right) dfs(root.right, nums); |
| 211 | + }; |
| 212 | + dfs(root1, l1); |
| 213 | + dfs(root2, l2); |
| 214 | + return l1.join(',') === l2.join(','); |
| 215 | +}; |
| 216 | +``` |
| 217 | + |
| 218 | +--- |
| 219 | +``` |
| 220 | +
|
| 221 | +Let me know if you'd like a download link or the file packaged for a GitHub-style repo. |
0 commit comments