资讯专栏INFORMATION COLUMN

leetcode100 Same Tree 引发的对遍历?算法的思考

cyrils / 3541人阅读

摘要:判断两棵树是否是相同题目要求传入两棵树的根节点,判断这两棵树是否相同此题的核心就在于如何遍历树。定义树的一种自然方式是递归的方式。一棵树是一些节点的集合,这个集合可以是空集。这个遍历算法可以用于场景如,计算一个节点的高度。

判断两棵树是否是相同

题目要求:传入两棵树的根节点,判断这两棵树是否相同
此题的核心就在于如何遍历树。一旦我们解决了这个问题,题目也就迎刃而解了。
下面就来介绍一下 关于树的一些基本知识

1.预备知识

树(tree)可以用几种方式定义。定义树的一种自然方式是递归的方式。一棵树是一些节点的集合,这个集合可以是空集。若不是空集,则树由叫做根(root)的节点r以及0个或多个非空子树T1,T2...Tk组成,这些子树中每一棵的根都被来自根r的一条有向的边(edge)所连结。

节点的深度: 从根到该节点的唯一的路径的长

二叉树: 一个每个节点都不能有多于两个儿子的树
题目中二叉树的节点表示为

</>复制代码

  1. class TreeNode{
  2. //节点的值
  3. int val;
  4. //左子节点,可以为null
  5. TreeNode left;
  6. //右子节点,可以为null
  7. TreeNode right;
  8. TreeNode(int x) { val = x; }
  9. }
2.二叉树的遍历方法

中序遍历:首先处理左子树,然后是当前节点,最后处理右子树。这个算法的总的运行时间是O(N),这是因为在树的每一个节点处进行的工作是常数时间,一共有n个节点,所以运行时间为O(N)

后序遍历:先处理左子树,再处理右子树,然后再处理当前节点。这个遍历算法可以用于场景如,计算一个节点的高度。

先序遍历:先处理当前节点,在处理左子树和右子树

层序遍历:所有深度为d的节点要在深度d+1的节点之前重组

</>复制代码

  1. //二叉树的中序遍历
  2. //递归
  3. void inOrder(TreeNode node){
  4. if(node!=null){
  5. inOrder(node.left);
  6. visit(node);
  7. inOrder(node.right);
  8. }
  9. }
  10. //二叉树的中序遍历
  11. //非递归 需要利用栈
  12. void inOrder(TreeNode node){
  13. Stack s = new Stack();
  14. TreeNode temp = node;
  15. for( ; ; ){
  16. //访问左节点
  17. while(temp!=null){
  18. s.push(temp);
  19. temp = temp.left;
  20. }
  21. //从左节点回来,访问右节点
  22. if(!s.isEmpty()){
  23. temp = s.pop();
  24. //访问当前节点
  25. System.out.println(temp.val);
  26. temp = temp.right;
  27. }else{
  28. return;
  29. }
  30. }
  31. }
  32. //二叉树的后序遍历
  33. void postOrder(TreeNode node){
  34. if(node!=null){
  35. postOrder(node.left);
  36. postOrder(node.right);
  37. visit(node);
  38. }
  39. }
  40. //二叉树的前序遍历
  41. void preOrder(TreeNode node){
  42. if(node!=null){
  43. visit(node);
  44. preOrder(node.left);
  45. preOrder(node.right);
  46. }
  47. }
3.本题的解法

</>复制代码

  1. **
  2. * @author rale
  3. * leetcode100
  4. * Given two binary trees, write a function to check if they are equal or not.
  5. * Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
  6. */
  7. public class SameTree {
  8. //递归 即当前值和左右子树都相等,则是相同的树,否则不是
  9. public boolean isSameTree(TreeNode p, TreeNode q) {
  10. if(p==null){
  11. return q==null?true:false;
  12. }else if(q==null){
  13. return p==null?true:false;
  14. }
  15. if(p.val == q.val){
  16. return isSameTree(p.left,q.left)&&isSameTree(p.right, q.right);
  17. }else{
  18. return false;
  19. }
  20. }
  21. //循环 中序遍历
  22. public boolean isSameTree2(TreeNode p, TreeNode q){
  23. Stack stackP = new Stack();
  24. Stack stackQ = new Stack();
  25. if(p!=null){
  26. stackP.push(p);
  27. }
  28. if(q!=null){
  29. stackQ.push(q);
  30. }
  31. while(!stackP.isEmpty() && !stackQ.isEmpty()){
  32. TreeNode tempP = stackP.pop();
  33. TreeNode tempQ = stackQ.pop();
  34. if(tempP.val!=tempQ.val){
  35. return false;
  36. }
  37. if(tempP.right!=null){
  38. stackP.push(tempP.right);
  39. }
  40. if(tempQ.right!=null){
  41. stackQ.push(tempQ.right);
  42. }
  43. if(stackP.size()!=stackQ.size()){
  44. return false;
  45. }
  46. if(tempP.left!=null){
  47. stackP.push(tempP.left);
  48. }
  49. if(tempQ.left!=null){
  50. stackQ.push(tempQ.left);
  51. }
  52. if(stackP.size()!=stackQ.size()){
  53. return false;
  54. }
  55. }
  56. return stackP.size()==stackQ.size();
  57. }
  58. public class TreeNode{
  59. int val;
  60. TreeNode left;
  61. TreeNode right;
  62. TreeNode(int x) { val = x; }
  63. }
  64. }


想要了解更多开发技术,面试教程以及互联网公司内推,欢迎关注我的微信公众号!将会不定期的发放福利哦~

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66848.html

相关文章

  • LeetCode 攻略 - 2019 年 7 月下半月汇总(100 题攻略)

    摘要:月下半旬攻略道题,目前已攻略题。目前简单难度攻略已经到题,所以后面会调整自己,在刷算法与数据结构的同时,攻略中等难度的题目。 Create by jsliang on 2019-07-30 16:15:37 Recently revised in 2019-07-30 17:04:20 7 月下半旬攻略 45 道题,目前已攻略 100 题。 一 目录 不折腾的前端,和咸鱼有什么区别...

    tain335 评论0 收藏0
  • LeetCode 攻略 - 2019 年 7 月上半月汇总(55 题攻略)

    摘要:微信公众号记录截图记录截图目前关于这块算法与数据结构的安排前。已攻略返回目录目前已攻略篇文章。会根据题解以及留言内容,进行补充,并添加上提供题解的小伙伴的昵称和地址。本许可协议授权之外的使用权限可以从处获得。 Create by jsliang on 2019-07-15 11:54:45 Recently revised in 2019-07-15 15:25:25 一 目录 不...

    warmcheng 评论0 收藏0
  • leetcode297. Serialize and Deserialize Binary Tree

    摘要:因此题目中的例子的中序遍历结果为。但是,标准的中序遍历并不能使我们将该结果反构造成一棵树,我们丢失了父节点和子节点之间的关系。这里我们也可以明显的看出来,中序遍历需要保存的空值远远多于广度优先遍历。 题目要求 Serialization is the process of converting a data structure or object into a sequence of ...

    desdik 评论0 收藏0
  • [Leetcode] Same Tree Symmetric Tree 相同树 对称树

    摘要:递归法复杂度时间空间递归栈空间思路如果两个根节点一个为空,一个不为空,或者两个根节点值不同,说明出现了不一样的地方,返回假。代码递归法复杂度时间空间递归栈空间思路其实和写法是一样的,是比较两个节点的两个左边,然后再比较两个节点的两个右边。 Same Tree Given two binary trees, write a function to check if they are eq...

    TZLLOG 评论0 收藏0
  • leetcode449. Serialize and Deserialize BST

    摘要:题目要求将二叉搜索树序列化和反序列化,序列化是指将树用字符串的形式表示,反序列化是指将字符串形式的树还原成原来的样子。假如二叉搜索树的节点较多,该算法将会占用大量的额外空间。 题目要求 Serialization is the process of converting a data structure or object into a sequence of bits so that...

    Honwhy 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<