第一次遇见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. 网络编程socket

    socket socket解释 socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄. 应用程序通常通过"套接字"向网络发出请求或者应答 ...

  2. Android--使用Notification在通知栏显示消息

    在一个Activity中点击按钮,产生一个通知栏消息通知. package cn.luxh.mynotice; import android.os.Bundle; import android.uti ...

  3. dd 命令实时进度监控

    dd脚本: #!/bin/sh if [ -d "${1}" ]; then echo "!mkdir" else } fi cd ${} name=`date ...

  4. 修改 sql server 2008R2的端口,配置防火墙允许远程访问SQL Server 2008 R2

    1.先修改 sql server 2008R2的端口号吧,1433经常成为别人入侵的端口,在sql server 配置管理器 -->sql server 网络配置-->MSSQLSERVE ...

  5. TextView里的文 html

    一.[Android实例]实现TextView里的文字有不同颜色 转eoe:http://www.eoeandroid.com/thread-4496-1-1.html import android. ...

  6. Linux-Apache+Mysql+PHP+PHPWind(重点Apache+PHP集成环境)

    整理Apache+Mysql+PHP+PHPWind(Apache+PHP集成环境) 一.情况简述: 1.虚拟机VM上面CentOS 2.全部yum安装(yum安装与源码安装的安装路径不同) 二.操作 ...

  7. 读书list

    1. TCP/IP 1.1 图解 TCP/IP 1.2 TCP/IP 详解 2. HTTP 2.1 HTTP 权威指南

  8. Begin using git (Part1) - Git的安装与配置

    Git提供了适用于Linux, Windows, OSX的客户端, 本节以Windows为例介绍基本安装与配置. 所需工具:msysgit, kdiff3. Get windows installer ...

  9. Android开发-API指南-Fragment

    Fragments 英文原文:http://developer.android.com/guide/components/fragments.html 采集日期:2014-12-31 在本文中 设计理 ...

  10. PMP考试--成本管理中常用的概念

    如果你对项目管理.系统架构有兴趣,请加微信订阅号"softjg",加入这个PM.架构师的大家庭 净现值(NPV)   Net Present Value 在项目计算期内,按行业基准 ...