个人记录-LeetCode 100. Same Tree
2017-04-20 14:51
363 查看
问题:
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.
这个问题比较简单,就是判断两颗树是否完全相等。
代码示例:
我们之间先序遍历比较一下即可。
代码类似于:
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 a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { if (p == null && q == null) { return true; } //先序遍历,先当前节点 //然后左子树,再右子树 if (p != null && q != null && p.val == q.val) { return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); } return false; } }
相关文章推荐
- 个人记录-LeetCode 101. Symmetric Tree
- 个人记录-LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal
- 个人记录-LeetCode 98. Validate Binary Search Tree
- 个人记录-LeetCode 104. Maximum Depth of Binary Tree
- 个人记录-LeetCode 108. Convert Sorted Array to Binary Search Tree
- 个人记录-LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode Everyday: 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 && 101. Symmetric Tree
- leetcode 100. Same Tree
- 【Leetcode】100. Same Tree
- [leetcode] 100. Same Tree 解题报告
- [Leetcode] 100. Same Tree
- LeetCode 100. Same Tree