zoukankan      html  css  js  c++  java
  • 【6_100】Same Tree

    Same Tree

    Total Accepted: 97481 Total Submissions: 230752 Difficulty: Easy

    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.

    Subscribe to see which companies asked this question

    这次卡在了递归的return使用上,原来可以一次return两个啊

    错误写法:

    else if(p != NULL && q != NULL && p->val == q->val) {
      isSameTree(p->left, q->left) ;

      isSameTree(p->right, q->right);
    }

    正确写法:

    else if(p != NULL && q != NULL && p->val == q->val) {
      return (isSameTree(p->left, q->left) && isSameTree(p->right, q->right));
    }

    这次是C语言

     1 /**
     2  * Definition for a binary tree node.
     3  * struct TreeNode {
     4  *     int val;
     5  *     struct TreeNode *left;
     6  *     struct TreeNode *right;
     7  * };
     8  */
     9 bool isSameTree(struct TreeNode* p, struct TreeNode* q) {
    10     if(p == NULL && q == NULL)
    11         return true;
    12     else if(p != NULL && q != NULL && p->val == q->val) {
    13             return (isSameTree(p->left, q->left) && isSameTree(p->right, q->right));
    14     }
    15     else
    16         return false;
    17 }
  • 相关阅读:
    无旋转Treap简介
    bzoj 4318 OSU!
    bzoj 1419 Red is good
    bzoj 4008 亚瑟王
    bzoj 1014 火星人prefix
    更多的莫队
    bzoj 3489 A simple rmq problem
    洛谷 2056 采花
    NOIP 2017 游(划水)记
    UVa 11997 K Smallest Sums
  • 原文地址:https://www.cnblogs.com/QingHuan/p/5041868.html
Copyright © 2011-2022 走看看