Skip to content

Commit

Permalink
feat: update solutions to lc problem: No.0872
Browse files Browse the repository at this point in the history
No.0872.Leaf-Similar Trees
  • Loading branch information
yanglbme authored Jul 9, 2022
1 parent 6182124 commit 9d0680d
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 124 deletions.
108 changes: 67 additions & 41 deletions solution/0800-0899/0872.Leaf-Similar Trees/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,14 @@
# self.left = left
# self.right = right
class Solution:
def leafSimilar(self, root1: TreeNode, root2: TreeNode) -> bool:
def dfs(root, leaves):
def leafSimilar(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool:
def dfs(root):
if root is None:
return
if root.left is None and root.right is None:
leaves.append(root.val)
return
dfs(root.left, leaves)
dfs(root.right, leaves)
l1, l2 = [], []
dfs(root1, l1)
dfs(root2, l2)
return l1 == l2
return []
ans = dfs(root.left) + dfs(root.right)
return ans or [root.val]

return dfs(root1) == dfs(root2)
```

### **Java**
Expand All @@ -102,50 +97,81 @@ class Solution:
*/
class Solution {
public boolean leafSimilar(TreeNode root1, TreeNode root2) {
List<Integer> l1 = new ArrayList<>();
List<Integer> l2 = new ArrayList<>();
dfs(root1, l1);
dfs(root2, l2);
List<Integer> l1 = dfs(root1);
List<Integer> l2 = dfs(root2);
return l1.equals(l2);
}

private void dfs(TreeNode root, List<Integer> leaves) {
if (root == null) return;
if (root.left == null && root.right == null) {
leaves.add(root.val);
return;
private List<Integer> dfs(TreeNode root) {
if (root == null) {
return new ArrayList<>();
}
List<Integer> ans = dfs(root.left);
ans.addAll(dfs(root.right));
if (ans.isEmpty()) {
ans.add(root.val);
}
dfs(root.left, leaves);
dfs(root.right, leaves);
return ans;
}
}
```

### **C++**

```cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool leafSimilar(TreeNode* root1, TreeNode* root2) {
return dfs(root1) == dfs(root2);
}

vector<int> dfs(TreeNode* root) {
if (!root) return {};
auto ans = dfs(root->left);
auto right = dfs(root->right);
ans.insert(ans.end(), right.begin(), right.end());
if (ans.empty()) ans.push_back(root->val);
return ans;
}
};
```

### **Go**

```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func leafSimilar(root1 *TreeNode, root2 *TreeNode) bool {
var l1, l2 []int
if root1 != nil {
dfs(root1, &l1)
}
if root2 != nil {
dfs(root2, &l2)
}
return reflect.DeepEqual(l1, l2)
}

func dfs(root *TreeNode, leaves *[]int) {
if root.Left == nil && root.Right == nil {
*leaves = append(*leaves, root.Val)
} else {
if root.Left != nil {
dfs(root.Left, leaves)
var dfs func(*TreeNode) []int
dfs = func(root *TreeNode) []int {
if root == nil {
return []int{}
}
if root.Right != nil {
dfs(root.Right, leaves)
ans := dfs(root.Left)
ans = append(ans, dfs(root.Right)...)
if len(ans) == 0 {
ans = append(ans, root.Val)
}
return ans
}
return reflect.DeepEqual(dfs(root1), dfs(root2))
}
```

Expand Down
108 changes: 67 additions & 41 deletions solution/0800-0899/0872.Leaf-Similar Trees/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,14 @@
# self.left = left
# self.right = right
class Solution:
def leafSimilar(self, root1: TreeNode, root2: TreeNode) -> bool:
def dfs(root, leaves):
def leafSimilar(self, root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool:
def dfs(root):
if root is None:
return
if root.left is None and root.right is None:
leaves.append(root.val)
return
dfs(root.left, leaves)
dfs(root.right, leaves)
l1, l2 = [], []
dfs(root1, l1)
dfs(root2, l2)
return l1 == l2
return []
ans = dfs(root.left) + dfs(root.right)
return ans or [root.val]

return dfs(root1) == dfs(root2)
```

### **Java**
Expand All @@ -86,50 +81,81 @@ class Solution:
*/
class Solution {
public boolean leafSimilar(TreeNode root1, TreeNode root2) {
List<Integer> l1 = new ArrayList<>();
List<Integer> l2 = new ArrayList<>();
dfs(root1, l1);
dfs(root2, l2);
List<Integer> l1 = dfs(root1);
List<Integer> l2 = dfs(root2);
return l1.equals(l2);
}

private void dfs(TreeNode root, List<Integer> leaves) {
if (root == null) return;
if (root.left == null && root.right == null) {
leaves.add(root.val);
return;
private List<Integer> dfs(TreeNode root) {
if (root == null) {
return new ArrayList<>();
}
List<Integer> ans = dfs(root.left);
ans.addAll(dfs(root.right));
if (ans.isEmpty()) {
ans.add(root.val);
}
dfs(root.left, leaves);
dfs(root.right, leaves);
return ans;
}
}
```

### **C++**

```cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool leafSimilar(TreeNode* root1, TreeNode* root2) {
return dfs(root1) == dfs(root2);
}

vector<int> dfs(TreeNode* root) {
if (!root) return {};
auto ans = dfs(root->left);
auto right = dfs(root->right);
ans.insert(ans.end(), right.begin(), right.end());
if (ans.empty()) ans.push_back(root->val);
return ans;
}
};
```

### **Go**

```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func leafSimilar(root1 *TreeNode, root2 *TreeNode) bool {
var l1, l2 []int
if root1 != nil {
dfs(root1, &l1)
}
if root2 != nil {
dfs(root2, &l2)
}
return reflect.DeepEqual(l1, l2)
}

func dfs(root *TreeNode, leaves *[]int) {
if root.Left == nil && root.Right == nil {
*leaves = append(*leaves, root.Val)
} else {
if root.Left != nil {
dfs(root.Left, leaves)
var dfs func(*TreeNode) []int
dfs = func(root *TreeNode) []int {
if root == nil {
return []int{}
}
if root.Right != nil {
dfs(root.Right, leaves)
ans := dfs(root.Left)
ans = append(ans, dfs(root.Right)...)
if len(ans) == 0 {
ans = append(ans, root.Val)
}
return ans
}
return reflect.DeepEqual(dfs(root1), dfs(root2))
}
```

Expand Down
26 changes: 26 additions & 0 deletions solution/0800-0899/0872.Leaf-Similar Trees/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool leafSimilar(TreeNode* root1, TreeNode* root2) {
return dfs(root1) == dfs(root2);
}

vector<int> dfs(TreeNode* root) {
if (!root) return {};
auto ans = dfs(root->left);
auto right = dfs(root->right);
ans.insert(ans.end(), right.begin(), right.end());
if (ans.empty()) ans.push_back(root->val);
return ans;
}
};
38 changes: 19 additions & 19 deletions solution/0800-0899/0872.Leaf-Similar Trees/Solution.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func leafSimilar(root1 *TreeNode, root2 *TreeNode) bool {
var l1, l2 []int
if root1 != nil {
dfs(root1, &l1)
}
if root2 != nil {
dfs(root2, &l2)
}
return reflect.DeepEqual(l1, l2)
}

func dfs(root *TreeNode, leaves *[]int) {
if root.Left == nil && root.Right == nil {
*leaves = append(*leaves, root.Val)
} else {
if root.Left != nil {
dfs(root.Left, leaves)
var dfs func(*TreeNode) []int
dfs = func(root *TreeNode) []int {
if root == nil {
return []int{}
}
if root.Right != nil {
dfs(root.Right, leaves)
ans := dfs(root.Left)
ans = append(ans, dfs(root.Right)...)
if len(ans) == 0 {
ans = append(ans, root.Val)
}
return ans
}
}
return reflect.DeepEqual(dfs(root1), dfs(root2))
}
22 changes: 11 additions & 11 deletions solution/0800-0899/0872.Leaf-Similar Trees/Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,20 @@
*/
class Solution {
public boolean leafSimilar(TreeNode root1, TreeNode root2) {
List<Integer> l1 = new ArrayList<>();
List<Integer> l2 = new ArrayList<>();
dfs(root1, l1);
dfs(root2, l2);
List<Integer> l1 = dfs(root1);
List<Integer> l2 = dfs(root2);
return l1.equals(l2);
}

private void dfs(TreeNode root, List<Integer> leaves) {
if (root == null) return;
if (root.left == null && root.right == null) {
leaves.add(root.val);
return;
private List<Integer> dfs(TreeNode root) {
if (root == null) {
return new ArrayList<>();
}
dfs(root.left, leaves);
dfs(root.right, leaves);
List<Integer> ans = dfs(root.left);
ans.addAll(dfs(root.right));
if (ans.isEmpty()) {
ans.add(root.val);
}
return ans;
}
}
Loading

0 comments on commit 9d0680d

Please sign in to comment.