LeetCode: isSameTree1 解题报告
2014-12-03 22:08
302 查看
isSameTree1
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.
View Code
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/tree/IsSameTree1.java
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.
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { // solution 1: public boolean isSameTree1(TreeNode p, TreeNode q) { if (p == null && q == null) { return true; } if (p == null || q == null) { return false; } return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right); } // Solution 2: public boolean isSameTree(TreeNode p, TreeNode q) { if (p == null && q == null) { return true; } if (p == null || q == null) { return false; } Stack<TreeNode> s1 = new Stack<TreeNode>(); Stack<TreeNode> s2 = new Stack<TreeNode>(); s1.push(p); s2.push(q); while (!s1.isEmpty() && !s2.isEmpty()) { TreeNode cur1 = s1.pop(); TreeNode cur2 = s2.pop(); // 弹出的节点的值必须相等 if (cur1.val != cur2.val) { return false; } // tree1的right节点,tree2的right节点,可以同时不为空,也可以同时为空,否则返回false. if (cur1.left != null && cur2.left != null) { s1.push(cur1.left); s2.push(cur2.left); } else if (!(cur1.left == null && cur2.left == null)) { return false; } // tree1的左节点,tree2的left节点,可以同时不为空,也可以同时为空,否则返回false. if (cur1.right != null && cur2.right != null) { s1.push(cur1.right); s2.push(cur2.right); } else if (!(cur1.right == null && cur2.right == null)) { return false; } } return true; } }
View Code
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/tree/IsSameTree1.java
相关文章推荐
- LeetCode Convert Sorted List to Binary Search Tree 解题报告
- 【LeetCode】257. Binary Tree Paths 解题报告
- [Leetcode] 270. Closest Binary Search Tree Value 解题报告
- 【LeetCode】Balanced Binary Tree 解题报告
- 【LeetCode】111.Minimum Depth of Binary Tree(Easy)解题报告
- 【LeetCode】100. Same Tree 解题报告
- [Leetcode] 105. Construct Binary Tree from Preorder and Inorder Traversal 解题报告
- [LeetCode]Binary Tree Level Order Traversal II,解题报告
- 【LeetCode】637.Average of Levels in Binary Tree(Easy)解题报告
- [leetcode] 235. Lowest Common Ancestor of a Binary Search Tree 解题报告
- [LeetCode]Binary Search Tree Iterator,解题报告
- [leetcode] 270. Closest Binary Search Tree Value 解题报告
- 【LeetCode】108.Convert Sorted Array to Binary Search Tree(Easy)解题报告
- 【LeetCode】Binary Tree Right Side View 解题报告
- LeetCode解题报告—— Unique Binary Search Trees & Binary Tree Level Order Traversal & Binary Tree Zigzag Level Order Traversal
- [leetcode] 108. Convert Sorted Array to Binary Search Tree 解题报告
- 【LeetCode】Construct Binary Tree from Preorder and Inorder Traversal 解题报告
- Leetcode 108. Convert Sorted Array to Binary Search Tree 有序数组转化BST 解题报告
- LeetCode(199) Binary Tree Right Side View解题报告
- 【LeetCode】104.Maximum Depth of Binary Tree(Easy)解题报告