第一次遇见Tree的题,拿到心慌,网上查了解题思路。写完就三行。。

最近努力学习一句话,学会喜欢自己。

题目:give two tree , you must judge if they are the same tree. ensure they are the same tree structure and node value.

开始思路:我想保存下tree的中序遍历,来判断tree是否相等,但是这块代码不会写。

解题思路:递归遍历两颗树,比较节点值是否相等。如果相等,则递归比较两颗树的leftSubTree 和 rightSubTree。注意:当出现任意一棵为Null 而另一棵不为Null 时,无条件返回false.

code:(Java)

public boolean isSameTree(TreeNode p, TreeNode q) {
if(p == null && q == null)
return true; // both trees are null
if(p == null && q != null || p != null && q == null || p.val != q.val)
       return false;
     // Any tree is null or their node value is different return isSameTree(p.left , q.left) && isSameTree(p.right , q.right);
// recursive compare their leftSubTree and rightSubTree
}

[leetcode]_Same Tree的更多相关文章

  1. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  2. LeetCode: Binary Tree Traversal

    LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...

  3. leetcode第一刷_Same Tree

    回来更博客的时候才发现.这道题不是跟推断树是不是对称的很相像吗.这个也是用了两个指针同一时候递归啊,有时候思维的局限真可笑. class Solution { public: bool isSameT ...

  4. [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历

    Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...

  5. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  6. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  7. [LeetCode] Binary Tree Right Side View 二叉树的右侧视图

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  8. [LeetCode] Binary Tree Upside Down 二叉树的上下颠倒

    Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...

  9. [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历

    Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...

随机推荐

  1. Remove Duplicates from Sorted List(链表)

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  2. 硬盘安装ubuntu120.04分区方案

    320G分区方案1/boot :256MB ext2swap :4G/ :40G ext4/tmp :5G ext4/var :20G ext4/usr :20G ext4/home :230GMB ...

  3. 创建一个提供数据 API 的 Node.js 网站

    创建站点目录 首先,创建一个文件夹用来保存你的站点文件,使用 mkdir 就可以了 PS C:\> mkdir mysite 然后,进入到这个文件夹进行下一步的操作. 创建包说明 使用记事本或者 ...

  4. Android 使用NineOldAndroids实现绚丽的ListView左右滑动删除Item效果

    本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/18311877) 今天还是给大家带来自定义控件的编写,自定义一个Lis ...

  5. 【测试】使用hr用户下的employees和departments表写一条SQL语句,(MG连接)

    SQL> select * from employees d, departments t where d.department_id=t.department_id; rows selecte ...

  6. 无法定位程序输入点ucrtbase.

    转:http://tieba.baidu.com/p/3848709732 后的解决方法是需要安装VC 2015 Redistributable,请自己选择相应的版本.百度云盘共享地址:http:// ...

  7. iOS 7中使用UINavigationController进行pop崩溃

    最近在一个项目中遇到一种情况,push到一个界面,如果那个界面未请求到数据,则直接pop回去,然而使用 [self.navigationController popViewControllerAnim ...

  8. [Oracle] 中的Temporary tablespace的作用

    临时表空间主要用途是在数据库进行排序运算[如创建索引.order by及group by.distinct.union/intersect/minus/.sort-merge及join.analyze ...

  9. css各兼容应该注意的问题

    1.div布局在ie浏览器和chrome浏览器,firefox浏览器不同,不如在div里面嵌套3个div,分别左中右,左边div的pading和margin在ie8以上都是几乎相同,ie8以下做内边距 ...

  10. Android创建自定义dialog方法详解-样式去掉阴影效果

    在自定义组件时,从已有组件源码中会很大收获.就拿progressDialog来说     间接父类是dialog,想了解dialog继承结构可以去百度,或者    从构造器来说ProgressDial ...