leetcode — same-tree
import java.util.Stack;
/**
* Source : https://oj.leetcode.com/problems/same-tree/
*
*
* Given two binary trees, write a function to check if they are equal or not.
*
* Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
*/
public class SameTree {
/**
* 比较两个是否相同
* 如果两棵树都为空,相同
* 如果只有一棵树为空,不相同
* 如果两棵树都不为空,则根节点相同,递归判断左右节点也要相同
*
* @param tree1
* @param tree2
* @return
*/
public boolean isSame (TreeNode tree1, TreeNode tree2) {
if (tree1 == null && tree2 == null) {
return true;
}
if ((tree1 == null && tree2 != null) || (tree1 != null && tree2 == null)) {
return false;
}
if (tree1.value != tree2.value) {
return false;
}
return isSame(tree1.leftChild, tree2.leftChild) && isSame(tree1.rightChild, tree2.rightChild);
}
/**
* 不使用递归,使用循环判断
*
* @param tree1
* @param tree2
* @return
*/
public boolean isSameByIterator (TreeNode tree1, TreeNode tree2) {
Stack<TreeNode> stack1 = new Stack<TreeNode>();
Stack<TreeNode> stack2 = new Stack<TreeNode>();
stack1.push(tree1);
stack2.push(tree2);
while (stack1.size() > 0 && stack2.size() > 0) {
TreeNode node1 = stack1.pop();
TreeNode node2 = stack2.pop();
if (node1 == null && node2 == null) {
continue;
}
if ((node1 == null && node2 != null) || (node1 != null && node2 == null)) {
return false;
}
if (node1.value != node2.value) {
return false;
}
stack1.push(node1.leftChild);
stack1.push(node1.rightChild);
stack2.push(node2.leftChild);
stack2.push(node2.rightChild);
}
return true;
}
public TreeNode createTree (char[] treeArr) {
TreeNode[] tree = new TreeNode[treeArr.length];
for (int i = 0; i < treeArr.length; i++) {
if (treeArr[i] == '#') {
tree[i] = null;
continue;
}
tree[i] = new TreeNode(treeArr[i]-'0');
}
int pos = 0;
for (int i = 0; i < treeArr.length && pos < treeArr.length-1; i++) {
if (tree[i] != null) {
tree[i].leftChild = tree[++pos];
if (pos < treeArr.length-1) {
tree[i].rightChild = tree[++pos];
}
}
}
return tree[0];
}
private class TreeNode {
TreeNode leftChild;
TreeNode rightChild;
int value;
public TreeNode(int value) {
this.value = value;
}
public TreeNode() {
}
}
public static void main(String[] args) {
SameTree sameTree = new SameTree();
char[] tree1 = new char[]{'1','2','3','#','#','4'};
char[] tree2 = new char[]{'1','2','3','#','#','4'};
char[] tree3 = new char[]{'1','2','3','2','#','4'};
System.out.println(sameTree.isSame(sameTree.createTree(tree1), sameTree.createTree(tree2)) + "---true");
System.out.println(sameTree.isSame(sameTree.createTree(tree1), sameTree.createTree(tree3)) + "---false");
System.out.println(sameTree.isSameByIterator(sameTree.createTree(tree1), sameTree.createTree(tree2)) + "---true");
System.out.println(sameTree.isSameByIterator(sameTree.createTree(tree1), sameTree.createTree(tree3)) + "---false");
}
}
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] 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 ...
- [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
随机推荐
- 通过excel获取一串连续的数字
输入一个格式的数字 点击按住右下角 拖动即可
- c# Exchange 收件箱获取。
public List<Email> GetInbox() { try { List<Email> lstEmails = new List<Email>(); F ...
- SDN网络虚拟化中有效协调的映射算法
来自论文An efficient and coordinated mapping algorithm in virtualized SDN networks,来自期刊<信息与电子工程前沿> ...
- PHP生成指定随机字符串的简单实现方法
/** * @param string $type * @param $length * @return string */ function randomString($type="num ...
- Gedit —— 推荐于NOI系列考试(NOIlinux)的轻量编程环境
由于Vim,Emacs上手艰难,Guide又特别难用,Anjuta还闪退 故推荐一款轻量化的编程环境:Gedit(文本编辑器) 配置方法: 1:在桌面上新建main.cpp,打开方式选择使用gedit ...
- Django model对象接口
Django model查询 # 直接获取表对应字段的值,列表嵌元组形式返回 Entry.objects.values_list('id', 'headline') #<QuerySet [(1 ...
- node02
1.使用已有的知识实现一个简单的登录和注册的界面 请求有请求接口有请求页面的,我们需要加以区分 以下是客户端代码 <!DOCTYPE html> <html lang="e ...
- node01
---恢复内容开始--- 1.node初体验 安装完成node,写好相应的js代码后,在cmd中node 文件名即可完成编译执行过程. 2.尝试使用node搭建一个简单服务器 //引入http模块 c ...
- Mesos源码分析(12): Mesos-Slave接收到RunTask消息
在前文Mesos源码分析(8): Mesos-Slave的初始化中,Mesos-Slave接收到RunTaskMessage消息,会调用Slave::runTask. void Slave::ru ...
- PHP workMan webSocket 转发器
PHP WorkerMan webSocket 功能演示===================================== 基本功能:实现页面websocket之间互相通讯 start_deb ...