第一次遇见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. 不能使用 snapshot 的解决方式

    http://www.mzone.cc/article/654.html 有两种方法可以解决: 1.第一种方法是在项目的pom文件中进行配置,如下: <repositories> < ...

  2. poj 2299 Ultra-QuickSort :归并排序求逆序数

    点击打开链接 Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 34676   Accepted ...

  3. EntityFramework 4使用存储过程分页

    CREATE PROC usp_OrgPage_SQL @pageIndex INT, @pageSize INT, @totalCount INT OUTPUT AS BEGIN SET @tota ...

  4. this.Invoke

    this.Invoke(new Action(() =>      {              }));

  5. css first-letter实现首字(字母)下沉效果

    css 首字下沉效果原理 首字下沉主要使用到css的first-letter伪元素,然后配合使用font-size和float来设置文字的样式即可实现. first-letter选择器选取指定元素文本 ...

  6. OC基础(19)

    类扩展(Class Extension) Block基本概念 typedef和Block Block注意事项 *:first-child { margin-top: 0 !important; } b ...

  7. 解决:对 PInvoke 函数的调用导致堆栈不对称问题 <转载>

    问题描述: 在使用托管代码调用非托管代码时,发生“对 PInvoke 函数“UseTwiHikVisionDllTest!UseTwiHikVisionDllTest.TwiHikVision::Ge ...

  8. 使用postman玩转接口测试

    (一)前言: 之前搞自动化接口测试,由于接口的特性,要验证接口返回xml中的数据,所以没找到合适的轮子,就自己用requests造了个轮子,用着也还行,不过就是case管理有些麻烦,近几天又回头看了看 ...

  9. 浅析JNI函数的注册过程

    我们在java中调用Native code的时候,一般是通过JNI来实现的,我们只需要在java类中加载本地.so库文件,并声明native方法,然后在需要调用的地方调用即可,至于java中nativ ...

  10. mysql学习笔记(sqlalchemy安装及简单使用)

    博主最近在研究接口API自动化测试,之前设计的通过excel来实现自动化测试的框架实际使用中还是有很多局限性 这次博主的思路是: 1 搭建接口API管理平台 支持数据库方便维护 2 自动化测试平台可直 ...