Identical Binary Tree

2016-09-19  本文已影响0人  一枚煎餅
Identical Binary Tree.png

解題思路 :

單純檢查兩棵樹的每一個點 透過起點再去 recursive call 檢查左跟右的子節點 一旦發現不同就直接回報 false 了

C++ code :

<pre><code>
/**

class Solution {

public:
/**
* @aaram a, b, the root of binary trees.
* @return true if they are identical, or false.
/
bool isIdentical(TreeNode
a, TreeNode* b) {
// Write your code here
if(!a && !b) return true;
if(!a || !b) return false;
if(a->val != b->val) return false;
return (isIdentical(a->left, b->left) && isIdentical(a->right, b->right));
}
};

上一篇 下一篇

猜你喜欢

热点阅读