[LeetCode] [C] 100. Same Tree
来源:互联网 发布:sql去除重复数据 编辑:程序博客网 时间:2024/06/06 06:58
To solve the problem about Binary Tree,
I still used Recursive Method.
Here, I still wrote another function to help traverse.
And I still declared the variable outside the two functions.
Simply speaking,
The program traversed these two trees synchronously,
and compared the value of every two tree nodes.
The Runtime of the program is 3 ms.
The length of the code is 476 Bytes.
bool is;void isSame (struct TreeNode* p, struct TreeNode* q) { if (!p && !q) return; else if ((!p && q) || (p && !q)) { is=false; return; } if (p->val!=q->val) { is=false; return; } else { if (p->left || q->left) isSame (p->left, q->left); if (p->right || q->right) isSame (p->right, q->right); }}bool isSameTree(struct TreeNode* p, struct TreeNode* q) { is=true; isSame(p,q); return is;}
Of course, there are better methods solving this problem.
More than half of people only used 0~1 ms.
阅读全文
0 0
- LeetCode 100. Same Tree (C++)
- [LeetCode] [C] 100. Same Tree
- leetcode: Same Tree (C++)
- LeetCode 100. Same Tree C语言
- [LeetCode练习题-C语言]100. Same Tree
- LeetCode 100. Same Tree 题解(C++)
- [LeetCode]100.Same Tree
- LeetCode 100. Same Tree
- 【LeetCode】100.Same Tree
- [Leetcode] 100. Same Tree
- [Leetcode] 100. Same Tree
- LeetCode --- 100. Same Tree
- LeetCode 100. Same Tree
- [leetcode] 100.Same Tree
- leetCode 100. Same Tree
- [LeetCode]100. Same Tree
- 100. Same Tree LeetCode
- LeetCode 100. Same Tree
- 【计算机系统组成】存储器知识要点
- class not found终结者
- 基于对象的数据筛选与排序(二)
- 第十二周LeetCode算法题三道
- 【Scikit-Learn 中文文档】内核岭回归
- [LeetCode] [C] 100. Same Tree
- 【CodeForces
- 在C# 中 textbox 文本的转换
- 栈的压入、弹出序列
- ndpi转换为wireshark的接口
- Ionic常用指令事件
- 神经网络能否代替决策树算法?
- 设计模式之禅11
- linux server运行图形化程序Xvfb