LeetCode -- Same Tree
来源:互联网 发布:mac用什么杀毒软件 编辑:程序博客网 时间:2024/05/21 10:15
题目描述:
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.
比较两个二叉树是否完全相同。
思路:
直接对两个树从根节点同时DFS,使用全局成员来记录是否相等即可。
实现代码:
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.
比较两个二叉树是否完全相同。
思路:
直接对两个树从根节点同时DFS,使用全局成员来记录是否相等即可。
实现代码:
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNode left; * public TreeNode right; * public TreeNode(int x) { val = x; } * } */public class Solution { public bool IsSameTree(TreeNode p, TreeNode q) { CompareTree(p, q); return _same; }private bool _same = true;private void CompareTree(TreeNode p , TreeNode q){if(!_same){return;}if(p == null && q == null){return ;}if(p == null && q != null || q == null && p != null || p.val != q.val){_same = false;return;}CompareTree(p.left, q.left);CompareTree(p.right, q.right);}}
1 0
- Leetcode - Tree - Same Tree
- [leetcode][tree] Same Tree
- LeetCode: Same Tree
- LeetCode Same Tree
- [Leetcode] Same Tree
- LeetCode : Same Tree
- [LeetCode] Same Tree
- leetcode 65: Same Tree
- Leetcode 100 Same Tree
- [leetcode] Same Tree
- [LeetCode]Same Tree
- [leetcode]Same Tree
- [Leetcode]Same Tree
- Leetcode: Same Tree
- [LeetCode] Same Tree
- LeetCode-Same Tree
- [leetcode] Same Tree
- LeetCode - Same Tree
- LeetCode -- Remove Element
- 4.2运算符重载
- Notepad++ php格式化 支持php高亮和自动自动函数补全的功能 +安装插件的方法
- 高并发与锁(一)
- Android studio 导入github上的源码库
- LeetCode -- Same Tree
- 天声人語 20151031 10月の言葉から
- 关于手机端浏览器显示兼容
- xcode集成Vungle IOS3.2.0版本视频广告的坑
- 让Mac读写移动硬盘
- github的注册及登陆后信息的设置
- Codeforces Round #279 (Div. 2) B. Queue(链表)
- oracle用户,权限,表空间管理
- document.execCommand 常用的方法.