zoukankan      html  css  js  c++  java
  • LeetCode: Same Tree

    一次过

     1 /**
     2  * Definition for binary tree
     3  * struct TreeNode {
     4  *     int val;
     5  *     TreeNode *left;
     6  *     TreeNode *right;
     7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution {
    11 public:
    12     bool isSameTree(TreeNode *p, TreeNode *q) {
    13         // Start typing your C/C++ solution below
    14         // DO NOT write int main() function
    15         if (!p && !q) return true;
    16         if ((!p && q) || (p && !q)) return false;
    17         return p->val == q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
    18     }
    19 };

     C#

     1 /**
     2  * Definition for a binary tree node.
     3  * public class TreeNode {
     4  *     public int val;
     5  *     public TreeNode left;
     6  *     public TreeNode right;
     7  *     public TreeNode(int x) { val = x; }
     8  * }
     9  */
    10 public class Solution {
    11     public bool IsSameTree(TreeNode p, TreeNode q) {
    12         if (p == null && q == null) return true;
    13         if (p == null || q == null) return false;
    14         return p.val == q.val && IsSameTree(p.left, q.left) && IsSameTree(p.right, q.right);
    15     }
    16 }
    View Code
  • 相关阅读:
    聪明人 & 普通人
    13种模型及方法论
    面向大规模商业系统的数据库设计和实践
    分治算法
    软件架构
    隐含前提思维模型
    Git回滚代码到某个commit
    使用arthas排查 test 问题
    Arthas
    docker 操作入门
  • 原文地址:https://www.cnblogs.com/yingzhongwen/p/3032642.html
Copyright © 2011-2022 走看看