zoukankan      html  css  js  c++  java
  • Leetcode 100. Same Tree

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

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

    Example 1:

    Input: 1 1
    / /
    2 3 2 3

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

    Output: true
    Example 2:

    Input: 1 1
    /
    2 2

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

    Output: false
    Example 3:

    Input: 1 1
    / /
    2 1 1 2

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

    Output: false。

    该题判断两棵树是否一样

    class Solution {
    public:
        bool isSameTree(TreeNode* p, TreeNode* q) {
            if(p==NULL && q==NULL) return true;
            if(p != NULL && q != NULL && p->val == q->val)
                return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
            return false;
        }
    };
  • 相关阅读:
    1015
    1016
    1014
    1002
    1010
    1006
    动态规划1001
    动态规划1002
    使用EF框架调用带有输出参数(output)的存储过程
    工程地质相关知识
  • 原文地址:https://www.cnblogs.com/littlepage/p/12271758.html
Copyright © 2011-2022 走看看