100.Same Tree(Swift待解)

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 Tree
Level: easy
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.
Inspired by [@JohnWeiGitHub](https://leetcode.com/discuss/3470/seeking-for-better-solution) and [@scott](https://leetcode.com/discuss/22197/my-non-recursive-method)
*/

import Foundation

class 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,空时再回头看这题。

时间: 2024-12-24 00:39:38

100.Same Tree(Swift待解)的相关文章

<LeetCode OJ> 100. Same Tree

100. Same Tree Total Accepted: 100129 Total Submissions: 236623 Difficulty: Easy 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 t

leetCode 100. Same Tree 树

100. 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. 题目大意: 判断两个二叉树是否完全相同.包括他们节点的内容. 代码如下:(递归版) /**  * De

100. Same Tree(C++)

100. 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. Subscribe to see which companies asked this questio

【100天爱上Swift】Day 1: Hello World!

前言 Swift已经出来好久了,7月份的语言排名已经到了第16位,可见从Apple的开发部门到各种Ios开发者都已经对这一语言投入了极大的热情,Swift在Ios平台上替代OC的势头势不可挡了,不然Apple也没有必要去开发一门新语言. 同时Google也在重写Android的API以适应用Go开发后期的Android设备,虽然没有Swift目前的进展大,但是看得出各大巨头也在通过推出新语言为后期不管是智能家具,穿戴设备,智能汽车的市场做布局了.总之这会一个势头,于是想对.net,C#的开发者说

linux下tree命令详解

1.description方法是NSObject自带的方法,包括类方法和对象方法 + (NSString *)description; // 默认返回 类名 - (NSString *)description; // 默认返回 <类名:内存地址> 2.默认情况下利用NSLog和%@输出对象的时返回的就是类名和内存地址 3.修改NSLog和%@的默认输出:重写类对象或者实例对象的description方法即可.因为NSLog函数进行打印的时候会自动调用description方法 /*******

Ext.Net学习笔记22:Ext.Net Tree 用法详解

Ext.Net学习笔记22:Ext.Net Tree 用法详解 上面的图片是一个简单的树,使用Ext.Net来创建这样的树结构非常简单,代码如下: <ext:TreePanel runat="server"> <Root> <ext:Node Text="根节点" Expanded="true"> <Children> <ext:Node Text="节点1" Expand

CentOS tree命令详解

inux下tree命令详解---linux以树状图逐级列出目录的内容命令 #############################################################################命令格式 tree <选项或者是参数> <分区或者是目录> #############################################################################(1) tree 最长使用的参数或者是选项 

LeetCode Javascript实现 100. Same Tree 171. Excel Sheet Column Number

100. Same Tree /** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode} p * @param {TreeNode} q * @return {boolean} */ var isSameTree = function(p, q) { if(p

Swift详解之三----------函数(你想知道的都在这里)

函数(你想知道的都在这里) 注:本文为作者自己总结,过于基础的就不再赘述 ,都是亲自测试的结果.如有错误或者遗漏的地方,欢迎指正,一起学习. 1. 函数的简单定义和调用 简单的无参函数就不再赘述 , name为形参 ,也是内部在数名 . func sayHello(name:String) ->String { return name+" say: hello" } 调用的时候也很简单 sayHello("zhangsan") 是不是很简单呀! 2.外部参数名