zoukankan      html  css  js  c++  java
  • leetcode| Same Tree

    Given two binary trees, write a function to check if they are equal or not.

    Two binary trees are considered equal if they are structurally identical and the nodes have the same value.

    题目:给定两个二叉树,判断他们是不是相等,相等的定义是结构以及各个节点的值都相同

    思路:不得不说,二叉树在leetcode上几乎都可以用递归求解,这个是有它的机构觉得,如我之前博文里的求二叉树的深度,反转二叉树,这道题也不例外,注意情况的判断处理,递归求解的代码如下:

    /**
    * Definition for a binary tree node.
    * public class TreeNode {
    * int val;
    * TreeNode left;
    * TreeNode right;
    * TreeNode(int x) { val = x; }
    * }
    */

    public boolean isSameTree(TreeNode p, TreeNode q) {

      if(p == null && q !=null) return false;

      if(q == null && p!=null) return false;

      if(q == null && q == null) return true;

      if(p.val == q.val) {

        return isSameTree(p.left,q.left) && isSameTree(p.right,q.right);

      }else{

        return false;

      }

    }

    StayHungry 求知若渴 StayFoolish 放低姿态
  • 相关阅读:
    flex布局
    媒体查询
    transform详细解释
    读取long类型数据
    Oracle中快速清空当前用户的所有表数据
    没有表头的csv文件怎么导入Kettle
    Kettle性能调优汇总
    oralce中特殊字符的查询
    数据的单值、多值、派生、简单、复合属性
    kettle学习
  • 原文地址:https://www.cnblogs.com/wujunjie/p/5687301.html
Copyright © 2011-2022 走看看