Skip to content

Latest commit

 

History

History
298 lines (248 loc) · 8.3 KB

File metadata and controls

298 lines (248 loc) · 8.3 KB

English Version

题目描述

二叉表达式树是一种表达算术表达式的二叉树。二叉表达式树中的每一个节点都有零个或两个子节点。 叶节点(有 0 个子节点的节点)表示操作数,非叶节点(有 2 个子节点的节点)表示运算符。在本题中,我们只考虑 '+' 运算符(即加法)。

给定两棵二叉表达式树的根节点 root1 和 root2 。如果两棵二叉表达式树等价,返回 true ,否则返回 false 。

当两棵二叉搜索树中的变量取任意值,分别求得的值都相等时,我们称这两棵二叉表达式树是等价的。

 

示例 1:

输入: root1 = [x], root2 = [x]
输出: true

示例 2:

输入:root1 = [+,a,+,null,null,b,c], root2 = [+,+,a,b,c]
输出:true
解释:a + (b + c) == (b + c) + a

示例 3:

输入: root1 = [+,a,+,null,null,b,c], root2 = [+,+,a,b,d]
输出: false
解释: a + (b + c) != (b + d) + a

 

提示:

  • 两棵树中的节点个数相等,且节点个数为范围 [1, 4999] 内的奇数。
  • Node.val 是 '+' 或小写英文字母。
  • 给定的树保证是有效的二叉表达式树。

 

进阶:当你的答案需同时支持 '-' 运算符(减法)时,你该如何修改你的答案

解法

Python3

# Definition for a binary tree node.
# class Node(object):
#     def __init__(self, val=" ", left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def checkEquivalence(self, root1: 'Node', root2: 'Node') -> bool:
        counter = [0] * 26

        def dfs(root, incr):
            if root:
                dfs(root.left, incr)
                dfs(root.right, incr)
                if root.val != '+':
                    counter[ord(root.val) - ord('a')] += incr

        dfs(root1, 1)
        dfs(root2, -1)
        return counter.count(0) == 26
# Definition for a binary tree node.
# class Node(object):
#     def __init__(self, val=" ", left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def checkEquivalence(self, root1: 'Node', root2: 'Node') -> bool:
        def calc(ans, left, right, op):
            for i in range(26):
                if op == '+':
                    ans[i] = left[i] + right[i]
                else:
                    ans[i] = left[i] - right[i]

        def dfs(root):
            ans = [0] * 26
            if not root:
                return ans
            if root.val in ['+', '-']:
                left, right = dfs(root.left), dfs(root.right)
                calc(ans, left, right, root.val)
            else:
                ans[ord(root.val) - ord('a')] += 1
            return ans

        return dfs(root1) == dfs(root2)

Java

/**
 * Definition for a binary tree node.
 * class Node {
 *     char val;
 *     Node left;
 *     Node right;
 *     Node() {this.val = ' ';}
 *     Node(char val) { this.val = val; }
 *     Node(char val, Node left, Node right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    private int[] counter;

    public boolean checkEquivalence(Node root1, Node root2) {
        counter = new int[26];
        dfs(root1, 1);
        dfs(root2, -1);
        for (int n : counter) {
            if (n != 0) {
                return false;
            }
        }
        return true;
    }

    private void dfs(Node root, int incr) {
        if (root == null) {
            return;
        }
        dfs(root.left, incr);
        dfs(root.right, incr);
        if (root.val != '+') {
            counter[root.val - 'a'] += incr;
        }
    }
}
/**
 * Definition for a binary tree node.
 * class Node {
 *     char val;
 *     Node left;
 *     Node right;
 *     Node() {this.val = ' ';}
 *     Node(char val) { this.val = val; }
 *     Node(char val, Node left, Node right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean checkEquivalence(Node root1, Node root2) {
        int[] ans1 = dfs(root1);
        int[] ans2 = dfs(root2);
        for (int i = 0; i < 26; ++i) {
            if (ans1[i] != ans2[i]) {
                return false;
            }
        }
        return true;
    }

    private int[] dfs(Node root) {
        int[] ans = new int[26];
        if (root == null) {
            return ans;
        }
        if (root.val == '+' || root.val == '-') {
            int[] left = dfs(root.left);
            int[] right = dfs(root.right);
            calc(ans, left, right, root.val);
        } else {
            ++ans[root.val - 'a'];
        }
        return ans;
    }

    private void calc(int[] ans, int[] left, int[] right, char op) {
        for (int i = 0; i < 26; ++i) {
            ans[i] = op == '+' ? left[i] + right[i] : left[i] - right[i];
        }
    }
}

C++

/**
 * Definition for a binary tree node.
 * struct Node {
 *     char val;
 *     Node *left;
 *     Node *right;
 *     Node() : val(' '), left(nullptr), right(nullptr) {}
 *     Node(char x) : val(x), left(nullptr), right(nullptr) {}
 *     Node(char x, Node *left, Node *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> counter;

    bool checkEquivalence(Node* root1, Node* root2) {
        counter.resize(26);
        dfs(root1, 1);
        dfs(root2, -1);
        return count(counter.begin(), counter.end(), 0) == 26;
    }

    void dfs(Node* root, int incr) {
        if (!root) return;
        dfs(root->left, incr);
        dfs(root->right, incr);
        if (root->val != '+') counter[root->val - 'a'] += incr;
    }
};
/**
 * Definition for a binary tree node.
 * struct Node {
 *     char val;
 *     Node *left;
 *     Node *right;
 *     Node() : val(' '), left(nullptr), right(nullptr) {}
 *     Node(char x) : val(x), left(nullptr), right(nullptr) {}
 *     Node(char x, Node *left, Node *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool checkEquivalence(Node* root1, Node* root2) {
        return dfs(root1) == dfs(root2);
    }

    vector<int> dfs(Node* root) {
        vector<int> ans(26);
        if (!root) return ans;
        if (root->val == '+' || root->val == '-')
        {
            auto left = dfs(root->left);
            auto right = dfs(root->right);
            calc(ans, left, right, root->val);
            return ans;
        }
        ++ans[root->val - 'a'];
        return ans;
    }

    void calc(vector<int>& ans, vector<int>& left, vector<int>& right, char op) {
        for (int i = 0; i < 26; ++i)
            ans[i] = op == '+' ? left[i] + right[i] : left[i] - right[i];
    }
};

...