lintcode:等价二叉树

等价二叉树

检查两棵二叉树是否等价。等价的意思是说,首先两棵二叉树必须拥有相同的结构,并且每个对应位置上的节点上的数都相等。

样例

    1             1
   / \           /   2   2   and   2   2
 /             /
4             4

就是两棵等价的二叉树。

    1             1
   / \           /   2   3   and   2   3
 /               4                 4

就不是等价的。

解题

树的结构相同,结点值相等

直接递归

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param a, b, the root of binary trees.
     * @return true if they are identical, or false.
     */
    public boolean isIdentical(TreeNode a, TreeNode b) {
        // Write your code here
        if(a == null && b == null){
             return true;
         }
         if(a == null && b != null || a!= null && b == null || a.val != b.val ){
             return false;
         }

         return isIdentical(a.left, b.left) && isIdentical(a.right, b.right);
    }

}
时间: 2024-08-08 01:08:15

lintcode:等价二叉树的相关文章

LeetCode951-翻转等价二叉树

问题:翻转等价二叉树 我们可以为二叉树 T 定义一个翻转操作,如下所示:选择任意节点,然后交换它的左子树和右子树. 只要经过一定次数的翻转操作后,能使 X 等于 Y,我们就称二叉树 X 翻转等价于二叉树 Y. 编写一个判断两个二叉树是否是翻转等价的函数.这些树由根节点 root1 和 root2 给出. 示例: 输入:root1 = [1,2,3,4,5,6,null,null,null,7,8], root2 = [1,3,2,null,6,4,5,null,null,null,null,8,

Lintcode 469. 等价二叉树

----------------------------------------------- AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */

等价二叉树

检查两棵二叉树是否等价.等价的意思是说,首先两棵二叉树必须拥有相同的结构,并且每个对应位置上的节点上的数都相等. 样例 1 1 / \ / 2 2 and 2 2 / / 4 4 就是两棵等价的二叉树. 1 1 / \ / 2 3 and 2 3 / 4 4 就不是等价的. /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * publi

LintCode_469 等价二叉树

题目 检查两棵二叉树是否等价.等价的意思是说,首先两棵二叉树必须拥有相同的结构,并且每个对应位置上的节点上的数都相等. 样例 1 1 / \ / 2 2 and 2 2 / / 4 4 就是两棵等价的二叉树. 1 1 / \ / 2 3 and 2 3 / 4 4 就不是等价的. C++代码 bool isIdentical(TreeNode* a, TreeNode* b) { // Write your code here if(!a && !b) { return true; } i

leetcode469:等价二叉树

1. 检查两棵二叉树是否等价.等价的意思是说,首先两棵二叉树必须拥有相同的结构,并且每个对应位置上的节点上的数都相等. 1 1 / \ / 2 2 and 2 2 / / 4 4 就是两棵等价的二叉树. 1 1 / \ / 2 3 and 2 3 / 4 4 就不是等价的. 2.思路: 1.首先,判断跟,为空就返回true 2.其次,当不想等时.一方为空,一方不为空.或者两方的值不相等 3.再次递归本方法:先左后右 代码:递归 class TreeNode { public int val; p

lintcode:二叉树的层次遍历

地址: http://lintcode.com/zh-cn/problem/binary-tree-level-order-traversal/ 借助队列来完成 class Solution { public: /* * @param root: A Tree * @return: Level order a list of lists of integer */ vector<vector<int>> levelOrder(TreeNode * root) { // write

Lintcode 97.二叉树的最大深度

--------------------------------- AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class S

LintCode之二叉树的最大节点

分治问题,可以把整棵树看做是由一颗颗只有三个节点组成的小树,一颗树的构成是根节点.左子树.右子树,这样只需要从左子树找出一个最大的节点,从右子树找出一个最大的节点,然后与根节点三个取个最大的,就是最终的结果了. AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * th

LintCode 68. 二叉树的后序遍历

题目:给出一棵二叉树,返回其节点值的后序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 2 / 3 返回 [3,2,1] 挑战 你能使用非递归实现么? 解:递归解,非递归以后补充 /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = thi