博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
100.Same Tree(Swift待解)
阅读量:5285 次
发布时间:2019-06-14

本文共 2676 字,大约阅读时间需要 8 分钟。

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.

 

思路:刚刚学习了二叉树,想到可能要分别遍历再比较,但对此题具体的实现还没有经验和具体代码结构。

参考代码:

/* https://leetcode.com/problems/same-tree/#100 Same TreeLevel: easyGiven 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.Inspired by [@JohnWeiGitHub](https://leetcode.com/discuss/3470/seeking-for-better-solution) and [@scott](https://leetcode.com/discuss/22197/my-non-recursive-method)*/import Foundationclass Easy_100_Same_Tree {    class Node {        var left: Node?        var right: Node?        var value: Int        init(value: Int, left: Node?, right: Node?) {            self.value = value            self.left = left            self.right = right        }    }    // t = O(N), average s = O(logN), worst s = O(N)    private class func isSameTree_recursion(p p: Node?, q: Node?) -> Bool {        if p == nil || q == nil {            return (p == nil && q == nil)        } else {            return p!.value == q!.value && isSameTree(p: p?.left, q: q?.left) && isSameTree(p: p?.right, q: q?.right)        }    }    // t = O(N), average s = O(logN), worst s = O(N)    private class func isSameTree_iteration(p p: Node?, q: Node?) -> Bool {        if p == nil || q == nil {            return (p == nil && q == nil)        }        var stack_p: [Node] = []        var stack_q: [Node] = []        stack_p.append(p!)        stack_q.append(q!)        while stack_p.isEmpty == false && stack_q.isEmpty == false {            let tmp_p: Node = stack_p.removeLast()            let tmp_q: Node = stack_q.removeLast()            if tmp_p.value != tmp_q.value {                return false            }            if tmp_p.left != nil {                stack_p.append(tmp_p.left!)            }            if tmp_q.left != nil {                stack_q.append(tmp_q.left!)            }            if stack_p.count != stack_q.count {                return false            }            if tmp_p.right != nil {                stack_p.append(tmp_p.right!)            }            if tmp_q.right != nil {                stack_q.append(tmp_q.right!)            }            if stack_p.count != stack_q.count {                return false            }        }        return stack_q.count == stack_q.count    }    class func isSameTree(p p: Node?, q: Node?) -> Bool {        return isSameTree_iteration(p: p, q: q)    }}

总结:时间原因,代码还未全部搞懂,树的初始化也有所不同,leetcode上还没AC,空时再回头看这题。

转载于:https://www.cnblogs.com/torrescx/p/5222188.html

你可能感兴趣的文章
poj100纪念
查看>>
NetWork——关于TCP协议的三次握手和四次挥手
查看>>
An easy problem
查看>>
MauiMETA工具的使用(一)
查看>>
LeetCode: Anagrams 解题报告
查看>>
Qt 中获取本机IP地址
查看>>
070102_赌博设计:概率的基本概念,古典概型
查看>>
IT人生的价值和意义 感觉真的有了
查看>>
JS DOM对象
查看>>
OGR – Merging Multiple SHP files
查看>>
创业公司该不该被收购?(转)
查看>>
sqlserver 行转列、列转行[转]
查看>>
【IScroll深入学习】解决IScroll疑难杂症
查看>>
python 数据类型
查看>>
108-PHP类成员protected和private成员属性不能被查看数值
查看>>
css控制height充满浏览器视口
查看>>
Linux 系统目录结构
查看>>
python学习之 - XML
查看>>
Laravel学习笔记(三)数据库 数据库迁移
查看>>
ORACLE查看并修改最大连接数
查看>>