LintCode: Identical Binary Tree

简介:

C++

复制代码
 1 /**
 2  * Definition of TreeNode:
 3  * class TreeNode {
 4  * public:
 5  *     int val;
 6  *     TreeNode *left, *right;
 7  *     TreeNode(int val) {
 8  *         this->val = val;
 9  *         this->left = this->right = NULL;
10  *     }
11  * }
12  */
13 class Solution {
14 public:
15     /**
16      * @aaram a, b, the root of binary trees.
17      * @return true if they are identical, or false.
18      */
19     bool isIdentical(TreeNode* a, TreeNode* b) {
20         // Write your code here
21         if (a == NULL && b == NULL) {
22             return true;
23         }
24         if (a == NULL || b == NULL) {
25             return false;
26         }
27         if (a->val != b->val) {
28             return false;
29         }
30         return isIdentical(a->left, b->left) && isIdentical(a->right, b->right);
31     }
32 };
复制代码

 


本文转自ZH奶酪博客园博客,原文链接:http://www.cnblogs.com/CheeseZH/p/5105595.html,如需转载请自行联系原作者

相关文章
LeetCode 110. Balanced Binary Tree
给定一颗二叉树,判断此树是不是平衡二叉树. 平衡二叉树的条件是:当前节点的左右子树高度差不超过1,且左右子树都是平衡二叉树.
60 0
LeetCode 110. Balanced Binary Tree
|
C++ Python
LeetCode 965. Univalued Binary Tree
LeetCode 965. Univalued Binary Tree
81 0
LeetCode 965. Univalued Binary Tree
LeetCode 105. Construct Binary Tree
给定一颗二叉树的前序和顺序遍历,构造原二叉树。 注意:您可以假设树中不存在重复项。
44 0
LeetCode 105. Construct Binary Tree
LeetCode 106. Construct Binary Tree
给定一颗二叉树的中序和后续遍历,构造原二叉树。 注意:您可以假设树中不存在重复项。
63 0
LeetCode 106. Construct Binary Tree
Leetcode-Easy 543. Diameter of Binary Tree
Leetcode-Easy 543. Diameter of Binary Tree
83 0
Leetcode-Easy 543. Diameter of Binary Tree