forked from mohitjain/leetcode_solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path101_symmetric_tree.rb
More file actions
55 lines (50 loc) · 1.17 KB
/
101_symmetric_tree.rb
File metadata and controls
55 lines (50 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Leetcode Problem: https://leetcode.com/problems/symmetric-tree/
#Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
#
# For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
#
# 1
# / \
# 2 2
# / \ / \
# 3 4 4 3
#
#
#
# But the following [1,2,2,null,3,null,3] is not:
#
# 1
# / \
# 2 2
# \ \
# 3 3
#
#
#
# Follow up: Solve it both recursively and iteratively.
#
# Definition for a binary tree node.
# class TreeNode
# attr_accessor :val, :left, :right
# def initialize(val = 0, left = nil, right = nil)
# @val = val
# @left = left
# @right = right
# end
# end
# @param {TreeNode} root
# @return {Boolean}
require_relative 'core/binary_tree'
def is_symmetric(root)
return true if root.nil?
symmetric?(root.left, root.right)
end
def symmetric? left, right
return left == right if left.nil? || right.nil?
return false if left.val != right.val
symmetric?(left.left, right.right) && symmetric?(left.right, right.left)
end
tree = BinaryTree.new [1, 2, 2, 3, 4, 4, 3]
p is_symmetric(tree.root)
tree = BinaryTree.new [1, 2, 2, nil, 3, nil, 3]
p is_symmetric(tree.root)