|
| 1 | +## 872. 叶子相似的树 |
| 2 | +> https://leetcode-cn.com/problems/leaf-similar-trees/ |
| 3 | +
|
| 4 | + |
| 5 | +### Java |
| 6 | +```java |
| 7 | +/* |
| 8 | + * @Author: Goog Tech |
| 9 | + * @Date: 2020-08-16 17:27:08 |
| 10 | + * @LastEditTime: 2020-08-16 17:47:02 |
| 11 | + * @Description: https://leetcode-cn.com/problems/leaf-similar-trees/ |
| 12 | + * @FilePath: \leetcode-googtech\#872. Leaf-Similar Trees\Solution.java |
| 13 | + * @WebSite: https://algorithm.show/ |
| 14 | + */ |
| 15 | + |
| 16 | +/* |
| 17 | + * @lc app=leetcode.cn id=872 lang=java |
| 18 | + * |
| 19 | + * [872] 叶子相似的树 |
| 20 | + */ |
| 21 | + |
| 22 | +// @lc code=start |
| 23 | +/** |
| 24 | + * Definition for a binary tree node. |
| 25 | + * public class TreeNode { |
| 26 | + * int val; |
| 27 | + * TreeNode left; |
| 28 | + * TreeNode right; |
| 29 | + * TreeNode(int x) { val = x; } |
| 30 | + * } |
| 31 | + */ |
| 32 | +class Solution { |
| 33 | + |
| 34 | + // DFS: 深度优先搜索 |
| 35 | + public boolean leafSimilar(TreeNode root1, TreeNode root2) { |
| 36 | + // 判断两个二叉树的叶值序列是否相同 |
| 37 | + String leftLeaf = dfs(root1, ""); |
| 38 | + String rightLeaf = dfs(root2, ""); |
| 39 | + return leftLeaf.equals(rightLeaf); |
| 40 | + } |
| 41 | + |
| 42 | + private String dfs(TreeNode root, String result) { |
| 43 | + // 判断当前节点是否为空 |
| 44 | + if(root == null) return result; |
| 45 | + // 判断当前节点是否为叶子节点,若是则对其进行拼接 |
| 46 | + if(root.left == null && root.right == null) { |
| 47 | + // 添加分隔符的目的: https://leetcode-cn.com/problems/leaf-similar-trees/comments/548544 |
| 48 | + result += '-'; |
| 49 | + result += result + root.val; |
| 50 | + return result; |
| 51 | + } |
| 52 | + // 递归遍历当前节点的左右孩子节点 |
| 53 | + return dfs(root.left, result) + dfs(root.right, result); |
| 54 | + } |
| 55 | +} |
| 56 | +// @lc code=end |
| 57 | +``` |
| 58 | + |
| 59 | +### Python |
| 60 | +```python |
| 61 | +''' |
| 62 | +Author: Goog Tech |
| 63 | +Date: 2020-08-16 17:28:02 |
| 64 | +LastEditTime: 2020-08-16 17:39:45 |
| 65 | +Description: https://leetcode-cn.com/problems/leaf-similar-trees/ |
| 66 | +FilePath: \leetcode-googtech\#872. Leaf-Similar Trees\Solution.py |
| 67 | +WebSite: https://algorithm.show/ |
| 68 | +''' |
| 69 | + |
| 70 | +# |
| 71 | +# @lc app=leetcode.cn id=872 lang=python |
| 72 | +# |
| 73 | +# [872] 叶子相似的树 |
| 74 | +# |
| 75 | + |
| 76 | +# @lc code=start |
| 77 | +# Definition for a binary tree node. |
| 78 | +# class TreeNode(object): |
| 79 | +# def __init__(self, x): |
| 80 | +# self.val = x |
| 81 | +# self.left = None |
| 82 | +# self.right = None |
| 83 | + |
| 84 | +class Solution(object): |
| 85 | + |
| 86 | + # 先序遍历 |
| 87 | + def leafSimilar(self, root1, root2): |
| 88 | + """ |
| 89 | + :type root1: TreeNode |
| 90 | + :type root2: TreeNode |
| 91 | + :rtype: bool |
| 92 | + """ |
| 93 | + # 用于存储两个二叉树的叶子节点 |
| 94 | + nums1, nums2 = [], [] |
| 95 | + def leaf(root, nums): |
| 96 | + # 判断当前节点是否为空并且是否为叶子节点,若是则将其添加到nums中 |
| 97 | + if root and not root.left and not root.right: |
| 98 | + nums.append(root.val) |
| 99 | + # 递归遍历当前节点的左右孩子节点 |
| 100 | + if root.left: leaf(root.left, nums) |
| 101 | + if root.right: leaf(root.right, nums) |
| 102 | + # 返回叶子节点列表 |
| 103 | + return nums |
| 104 | + # 判断两个二叉树的叶值序列是否相同 |
| 105 | + return leaf(root1, nums1) == leaf(root2, nums2) |
| 106 | +# @lc code=end |
| 107 | +``` |
0 commit comments