Last active
September 23, 2019 17:24
-
-
Save lbvf50mobile/247c9f9576924e611ac62dab2c4e9b70 to your computer and use it in GitHub Desktop.
isSymmetric
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Definition for a binary tree node. | |
# class TreeNode | |
# attr_accessor :val, :left, :right | |
# def initialize(val) | |
# @val = val | |
# @left, @right = nil, nil | |
# end | |
# end | |
# | |
# Runtime: 28 ms, faster than 98.04% of Ruby online submissions for Symmetric Tree. | |
# Memory Usage: 9.5 MB, less than 100.00% of Ruby online submissions for Symmetric Tree. | |
# @param {TreeNode} root | |
# @return {Boolean} | |
def is_symmetric(root) | |
return true if root.nil? | |
l,r = [],[] | |
l.push(root.left) | |
r.push(root.right) | |
while 0 != l.size do | |
x,y = l.shift(),r.shift() | |
return false unless (x.nil? && y.nil?) || (x && y && x.val == y.val) | |
l.push(x.left, x.right) if x | |
r.push(y.right, y.left) if y | |
end | |
true | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for a binary tree node. | |
* type TreeNode struct { | |
* Val int | |
* Left *TreeNode | |
* Right *TreeNode | |
* } | |
https://leetcode.com/problems/symmetric-tree/ | |
Runtime: 0 ms, faster than 100.00% of Go online submissions for Symmetric Tree. | |
Memory Usage: 3 MB, less than 25.00% of Go online submissions for Symmetric Tree. | |
*/ | |
func isSymmetric(root *TreeNode) bool { | |
if nil == root { | |
return true | |
} | |
var left, right [] * TreeNode | |
var x,y * TreeNode | |
left = append(left, root.Left) | |
right = append(right, root.Right) | |
for 0 != len(left){ | |
x, left = left[0], left[1:] | |
y, right = right[0], right[1:] | |
if !((x == nil && y == nil ) || (x != nil && y != nil && x.Val == y.Val)){ | |
return false | |
} | |
if x != nil{ | |
left = append(left, x.Left, x.Right) | |
} | |
if y != nil { | |
right = append(right, y.Right, y.Left) | |
} | |
} | |
return true | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for a binary tree node. | |
* function TreeNode(val) { | |
* this.val = val; | |
* this.left = this.right = null; | |
* } | |
*/ | |
/** | |
* @param {TreeNode} root | |
* @return {boolean} | |
Runtime: 72 ms, faster than 17.60% of JavaScript online submissions for Symmetric Tree. | |
Memory Usage: 36.1 MB, less than 20.00% of JavaScript online submissions for Symmetric Tre | |
*/ | |
var isSymmetric = function(root) { | |
if(root == null) return true | |
let [l,r] = [[],[]] | |
l.push(root.left) | |
r.push(root.right) | |
while(0 != l.length){ | |
let [x,y] = [l.shift(),r.shift()] | |
if (!((x == null && y == null) || ( x && y && x.val == y.val))){ | |
return false | |
} | |
if(x){ | |
l.push(x.left, x.right) | |
} | |
if(y){ | |
r.push(y.right,y.left) | |
} | |
} | |
return true | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Definition for a binary tree node. | |
# class TreeNode: | |
# def __init__(self, x): | |
# self.val = x | |
# self.left = None | |
# self.right = None | |
''' | |
Runtime: 40 ms, faster than 75.18% of Python3 online submissions for Symmetric Tree. | |
Memory Usage: 14 MB, less than 5.17% of Python3 online submissions for Symmetric Tree. | |
https://leetcode.com/problems/symmetric-tree/submissions/ | |
''' | |
class Solution: | |
def isSymmetric(self, root: TreeNode) -> bool: | |
if root is None: return True | |
l,r = [],[] | |
l.append(root.left) | |
r.append(root.right) | |
while 0 != len(l): | |
x,y = l.pop(0), r.pop(0) | |
if not ((x == None and y == None) or ( x and y and x.val == y.val)): return False | |
if x: l.extend([x.left,x.right]) | |
if y: r.extend([y.right,y.left]) | |
return True | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment