[leetcode]_Same Tree
第一次遇见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的更多相关文章
- LeetCode:Binary Tree Level Order Traversal I II
LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...
- LeetCode: Binary Tree Traversal
LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...
- leetcode第一刷_Same Tree
回来更博客的时候才发现.这道题不是跟推断树是不是对称的很相像吗.这个也是用了两个指针同一时候递归啊,有时候思维的局限真可笑. class Solution { public: bool isSameT ...
- [LeetCode] Binary Tree Vertical Order Traversal 二叉树的竖直遍历
Given a binary tree, return the vertical order traversal of its nodes' values. (ie, from top to bott ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- [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 ...
- [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 ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
随机推荐
- dede数据库类使用方法$dsql【转】
http://www.cnblogs.com/xcxc/p/3601909.html dedecms的数据库操作类,非常实用,在二次开发中尤其重要,这个数据库操作类说明算是奉献给大家的小礼物了. 引入 ...
- [ActionScript 3.0] AS3 GUID(全局唯一标识符)
package com.controls { import flash.display.Sprite; import flash.system.Capabilities; public class G ...
- [ActionScript 3.0] AS3中的位图(BitmapData)应用
1.位图使用(模糊)滤镜 //创建一个矩形区域的BitmapData var bmd:BitmapData = new BitmapData(80, 30, false, 0xffffff); //画 ...
- MyEclipse运行时自动保存
今天第一次用MyEclipse,我发现我的代码明明修改了,但运行结果发现总是修改前的代码结果.后来发现,是代码修改后必须保存,再点运行.这个功能明显不合适,所以需要更改MyEclipse的配置.红框是 ...
- 范式(Oracle)
三范式 ------------数据库的三范式-------------- (1).要有主键,列不可分 (2).不能存在部分依赖:当有多个字段联合起来作为主键的时候,不是主键的字段不能部分依赖于主键中 ...
- 页面设计--RadioButton
RadioButton单选控件支持多分组模式 属性如下图 设计: web显示效果图:
- python连接hiveserver2
sudo pip install pyhs2 网上找的例子: #!/usr/bin/env python # -*- coding: utf-8 -*- # hive util with hive s ...
- js压缩反压缩
JavaScript unpacker and beautifier JavaScript Beautifier http://prettydiff.com/?m=beautify&s=htt ...
- php 获取图片主要颜色的方法
本文章向码农们介绍php 获取图片主要颜色的方法,主要涉及php针对图片的相关操作技巧,需要的码农可以参考一下. $i = imagecreatefromjpeg("image.jpg&qu ...
- DP三角形
Hrbust1038 http://acm.hrbust.edu.cn/index.php?m=ProblemSet&a=showProblem&problem_id=1038 // ...