Same Tree
来源:互联网 发布:北大青鸟 液晶层显编程 编辑:程序博客网 时间:2024/05/17 01:35
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.
traversal two tree at the same time, decide by cases: both exist, both not exist, one exist. Preorder should be the best
/** * Definition for binary tree * 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 (!isSame(p,q)) return false; if (p&&!isSameTree(p->left,q->left)) return false; if (p&&!isSameTree(p->right,q->right)) return false; return true; } bool isSame(TreeNode* p, TreeNode* q){ if ((p && q && p->val==q->val)|| !p&&!q) return true; return false; }};
0 0
- Same Tree
- Same Tree
- Same Tree
- Same Tree
- Same Tree
- Same Tree
- Same Tree
- Same Tree
- Same Tree
- Same Tree
- Same tree
- Same Tree
- Same Tree
- Same Tree
- Same Tree
- Same Tree
- Same Tree
- Same Tree
- Irish Lamb Stew
- Android 联系人的读取查询添加
- 解决错误:Assertion failed: hunk, file patch.c, line 321
- 如何查看Android设备的CPU架构信息
- SQL查询入门(下篇)
- Same Tree
- 使用 Google Guava 美化你的 Java 代码:1~4
- DevExpress.XtraTreeList.TreeList (树控件使用说明)
- ubuntu 删除network-manager 后无法上网的问题
- windows环境C语言修改系统时间-WinAPI总结
- 解决如何通过循环来使用数据库的值设置jsp的select标签的option值
- 斜视角的讨论
- struts2和servlet区别
- shell 脚本中的入参获取与判断