100.相同的树(Same Tree)

原创 2018年04月17日 18:34:26

题目描述

给定两个二叉树,写一个函数来检查它们是否相同。

如果两棵树在结构上相同并且节点具有相同的值,则认为它们是相同的。

示例 1:

输入 :     1          1
          / \       / \
         2   3     2   3

        [1,2,3],   [1,2,3]

输出: true

示例 2:

输入  :    1          1
          /           \
         2             2

        [1,2],     [1,null,2]

输出: false

例 3:

输入 :     1          1
          / \       / \
         2   1     1   2

        [1,2,1],   [1,1,2]

输出: false

解题思路

    /**
     * 递归地解决方法
     * @param p
     * @param q
     * @return
     */
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p == null && q == null) return true;   //说明已经到了叶子结点了
        if(p == null || q == null) return false;  //有一个结点为空,另外一个结点不为空

        if (p.val == q.val){
            return isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
        }
        return false;
    }
    /**
     * 用两个栈解决问题,非递归
     * @param p
     * @param q
     * @return
     */
    public boolean isSameTree(TreeNode p, TreeNode q) {
        Stack<TreeNode> stack_p = new Stack <TreeNode> ();
        Stack<TreeNode> stack_q = new Stack <TreeNode> ();

        if (p != null) stack_p.push( p ) ;
        if (q != null) stack_q.push( q ) ;

        while (!stack_p.isEmpty() && !stack_q.isEmpty()) {
            TreeNode pn = stack_p.pop() ;
            TreeNode qn = stack_q.pop() ;

            if (pn.val != qn.val) return false ;   //如果值不等,直接返回false

            if (pn.left != null) stack_p.push(pn.left) ;
            if (qn.left != null) stack_q.push(qn.left) ;
            if (stack_p.size() != stack_q.size()) return false ;

            if (pn.right != null) stack_p.push(pn.right) ;
            if (qn.right != null) stack_q.push(qn.right) ;
            if (stack_p.size() != stack_q.size()) return false ;
        }
        return stack_p.size() == stack_q.size() ;
    }

下面的不是我的代码,可以参考一下;

     public boolean isSameTree(TreeNode p, TreeNode q) {        
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(p);
        queue.add(q);
        while(!queue.isEmpty()){
            TreeNode f = queue.poll();
            TreeNode s = queue.poll();
            if(f == null && s == null){
                continue;
            }else if(f == null || s == null || f.val != s.val){
                return false;
            }
            queue.add(f.left);
            queue.add(s.left);
            queue.add(f.right);
            queue.add(s.right);
        }
        return true;
    }

第3周:决策树模型(CART)、基于树的集成学习算法(随机森林、GBDT)-人工智能工程师直通车

-
  • 1970年01月01日 08:00

[LeetCode]100. Same Tree(相同树)

100. Same TreeGiven two binary trees, write a function to check if they are equal or not. 给定两个二叉树,写...
  • whl_program
  • whl_program
  • 2017-04-20 00:34:36
  • 253

100. Same Tree [easy] (Python)

题目链接https://leetcode.com/problems/same-tree/题目原文 Given two binary trees, write a function to check...
  • coder_orz
  • coder_orz
  • 2016-05-13 22:13:29
  • 1310

100. Same Tree Leetcode Python

Given two binary trees, write a function to check if they are equal or not. Two binary trees are c...
  • hyperbolechi
  • hyperbolechi
  • 2015-01-15 23:33:34
  • 449

LeetCode 100 — Same Tree(C++ Java Python)

题目:http://oj.leetcode.com/problems/same-tree/ Given two binary trees, write a function to check if...
  • dragon_dream
  • dragon_dream
  • 2014-04-01 20:22:21
  • 1934

LeetCode(100)Same Tree

现在开始按照AC率降序刷题,接下来的这几道题目确实都是挺简单的。 题目如下: Given two binary trees, write a function to check if they are...
  • feliciafay
  • feliciafay
  • 2014-01-09 06:41:21
  • 3500

[LeetCode-100]Same Tree(java)

Given two binary trees, write a function to check if they are equal or not. Two binary trees are co...
  • xionghuixionghui
  • xionghuixionghui
  • 2016-10-08 20:48:41
  • 444

【LeetCode】100. Same Tree 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280Subject 出处:https://leetcode.com/pro...
  • crazy1235
  • crazy1235
  • 2016-05-30 22:06:42
  • 8441

LeetCode 100 Same Tree(Python详解及实现)

【题目】 Given two binary trees, write a function tocheck if they are equal or not.   Two binary tree...
  • yangjingjing9
  • yangjingjing9
  • 2017-08-11 16:52:26
  • 127

【leetcode c++】100 Same Tree

Same Tree   Given two binary trees, write a function tocheck if they are equal or not. Two binary...
  • hqq39
  • hqq39
  • 2015-07-27 14:34:06
  • 403
收藏助手
不良信息举报
您举报文章:100.相同的树(Same Tree)
举报原因:
原因补充:

(最多只允许输入30个字)