当前位置

网站首页> 程序设计 > 开源项目 > 程序开发 > 浏览文章

[LintCode] Identical Binary Tree - Road to Glory

作者:小梦 来源: 网络 时间: 2024-02-10 阅读:

problem

Check if two binary trees are identical. Identical means the two binary trees have the same structure and every identical position has the same value.

Example

    1 1   / \           / \  2   2   and   2   2 / /4 4are identical.    1 1   / \           / \  2   3   and   2   3 /   \4     4are not identical.

Solution

public class solution {    public boolean isIdentical(TreeNode a, TreeNode b) {        if (a == null && b == null) return true;        if (a == null || b == null) return false;        return a.val == b.val && isIdentical(a.left, b.left) && isIdentical(a.right, b.right);    }}

热点阅读

网友最爱