LeetCode Same Tree 简单,简洁的递归解法

来源:互联网 发布:c语言国家二级考试时间 编辑:程序博客网 时间:2024/06/05 16:18

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.

非常简单

1.如果p或q为空,如果同时为空则为真,否则为假
2.如果两者都不空,则为真的条件是当前根节点相等,并且递归的调用左子树和右子树。

/** * Definition for a binary tree node. * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public:    bool isSameTree(TreeNode* p, TreeNode* q) {       if (p == NULL || q == NULL) return (p == q);       return (p->val == q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right));    }};
0 0
原创粉丝点击