Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6f07645

Browse files
feat: solve No.872
1 parent 5b48494 commit 6f07645

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

‎801-900/872. Leaf-Similar Trees.md‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# 872. Leaf-Similar Trees
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Tree, Depth-First Search, Binary Tree.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
Consider all the leaves of a binary tree, from left to right order, the values of those leaves form a **leaf value sequence****.**
10+
11+
12+
![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/16/tree.png)
13+
14+
15+
For example, in the given tree above, the leaf value sequence is `(6, 7, 4, 9, 8)`.
16+
17+
Two binary trees are considered **leaf-similar** if their leaf value sequence is the same.
18+
19+
Return `true` if and only if the two given trees with head nodes `root1` and `root2` are leaf-similar.
20+
21+
22+
Example 1:
23+
24+
![](https://assets.leetcode.com/uploads/2020/09/03/leaf-similar-1.jpg)
25+
26+
```
27+
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]
28+
Output: true
29+
```
30+
31+
Example 2:
32+
33+
![](https://assets.leetcode.com/uploads/2020/09/03/leaf-similar-2.jpg)
34+
35+
```
36+
Input: root1 = [1,2,3], root2 = [1,3,2]
37+
Output: false
38+
```
39+
40+
41+
**Constraints:**
42+
43+
44+
45+
- The number of nodes in each tree will be in the range `[1, 200]`.
46+
47+
- Both of the given trees will have values in the range `[0, 200]`.
48+
49+
50+
51+
## Solution
52+
53+
```javascript
54+
/**
55+
* Definition for a binary tree node.
56+
* function TreeNode(val, left, right) {
57+
* this.val = (val===undefined ? 0 : val)
58+
* this.left = (left===undefined ? null : left)
59+
* this.right = (right===undefined ? null : right)
60+
* }
61+
*/
62+
/**
63+
* @param {TreeNode} root1
64+
* @param {TreeNode} root2
65+
* @return {boolean}
66+
*/
67+
var leafSimilar = function(root1, root2) {
68+
var arr1 = [];
69+
var arr2 = [];
70+
getLeafNodes(root1, arr1);
71+
getLeafNodes(root2, arr2);
72+
return arr1.length === arr2.length
73+
&& arr1.every((item, index) => item === arr2[index]);
74+
};
75+
76+
var getLeafNodes = function(root, result) {
77+
if (!root.left && !root.right) result.push(root.val);
78+
root.left && getLeafNodes(root.left, result);
79+
root.right && getLeafNodes(root.right, result);
80+
};
81+
```
82+
83+
**Explain:**
84+
85+
nope.
86+
87+
**Complexity:**
88+
89+
* Time complexity : O(n).
90+
* Space complexity : O(n).

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /