Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8ae386d

Browse files
zhangzz2015gitbook-bot
authored andcommitted
GitBook: [greyireland#8] No subject
1 parent 177d681 commit 8ae386d

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

‎data_structure/binary_tree.md‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,9 +514,38 @@ bool maxDepth(root *TreeNode, int& depth) int {
514514
515515
> 一般工程中,结果通过两个变量来返回,不建议用一个变量表示两种含义
516516
517+
#### 100. Same Tree
518+
519+
[Same-Tree](https://leetcode.com/problems/same-tree/)
520+
521+
思路:左子树相同,右子树相同,root相同,才是相同的数,考虑分治,后序遍历
522+
523+
```cpp
524+
//
525+
class Solution {
526+
public:
527+
bool isSameTree(TreeNode* p, TreeNode* q) {
528+
529+
if(p==NULL && q == NULL) // both empty is same.
530+
return true;
531+
if(p==NULL || q==NULL) // one is empty other is not. false.
532+
return false;
533+
if(p->val!=q->val)
534+
return false;
535+
536+
if(isSameTree(p->left, q->left)==false)
537+
return false;
538+
if(isSameTree(p->right, q->right) == false)
539+
return false;
540+
541+
return true;
542+
}
543+
};
544+
```
545+
517546
#### 124. binary-tree-maximum-path-sum
518547

519-
[binary-tree-maximum-path-sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/)
548+
[binary-tree-maximum-path-sum](https://leetcode.com/problems/same-tree/)
520549

521550
> 给定一个**非空**二叉树,返回其最大路径和。
522551

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /