mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-27 21:58:57 +01:00
fix syntax of data_structures
This commit is contained in:
parent
04fd348291
commit
9c1823739c
5 changed files with 54 additions and 55 deletions
|
@ -76,5 +76,4 @@ class SingleList
|
||||||
def isEmpty()
|
def isEmpty()
|
||||||
return (@head==nil)
|
return (@head==nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
|
@ -1,23 +1,23 @@
|
||||||
# Definition for a binary tree node.
|
# Definition for a binary tree node.
|
||||||
# class TreeNode
|
# class TreeNode
|
||||||
# attr_accessor :val, :left, :right
|
# attr_accessor :val, :left, :right
|
||||||
# def initialize(val)
|
# def initialize(val)
|
||||||
# @val = val
|
# @val = val
|
||||||
# @left, @right = nil, nil
|
# @left, @right = nil, nil
|
||||||
# end
|
# end
|
||||||
# end
|
# end
|
||||||
|
|
||||||
# @param {TreeNode} root
|
# @param {TreeNode} root
|
||||||
# @return {Integer[]}
|
# @return {Integer[]}
|
||||||
def inorder_traversal(root)
|
def inorder_traversal(root)
|
||||||
ans = []
|
ans = []
|
||||||
def traverse(node, ans)
|
def traverse(node, ans)
|
||||||
if node != nil
|
if node != nil
|
||||||
traverse(node.left, ans)
|
traverse(node.left, ans)
|
||||||
ans.push(node.val)
|
ans.push(node.val)
|
||||||
traverse(node.right,ans)
|
traverse(node.right, ans)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
traverse(root,ans)
|
end
|
||||||
return ans
|
traverse(root, ans)
|
||||||
|
return ans
|
||||||
end
|
end
|
|
@ -1,20 +1,20 @@
|
||||||
# Definition for a binary tree node.
|
# Definition for a binary tree node.
|
||||||
# class TreeNode
|
# class TreeNode
|
||||||
# attr_accessor :val, :left, :right
|
# attr_accessor :val, :left, :right
|
||||||
# def initialize(val)
|
# def initialize(val)
|
||||||
# @val = val
|
# @val = val
|
||||||
# @left, @right = nil, nil
|
# @left, @right = nil, nil
|
||||||
# end
|
# end
|
||||||
# end
|
# end
|
||||||
|
|
||||||
# @param {TreeNode} root
|
# @param {TreeNode} root
|
||||||
# @return {TreeNode}
|
# @return {TreeNode}
|
||||||
def invert_tree(root)
|
def invert_tree(root)
|
||||||
if root == nil
|
if root == nil
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
temp = root.left
|
temp = root.left
|
||||||
root.left = invert_tree(root.right)
|
root.left = invert_tree(root.right)
|
||||||
root.right = invert_tree(temp)
|
root.right = invert_tree(temp)
|
||||||
return root
|
return root
|
||||||
end
|
end
|
|
@ -1,23 +1,23 @@
|
||||||
# Definition for a binary tree node.
|
# Definition for a binary tree node.
|
||||||
# class TreeNode
|
# class TreeNode
|
||||||
# attr_accessor :val, :left, :right
|
# attr_accessor :val, :left, :right
|
||||||
# def initialize(val)
|
# def initialize(val)
|
||||||
# @val = val
|
# @val = val
|
||||||
# @left, @right = nil, nil
|
# @left, @right = nil, nil
|
||||||
# end
|
# end
|
||||||
# end
|
# end
|
||||||
|
|
||||||
# @param {TreeNode} root
|
# @param {TreeNode} root
|
||||||
# @return {Integer[]}
|
# @return {Integer[]}
|
||||||
def postorder_traversal(root)
|
def postorder_traversal(root)
|
||||||
ans = []
|
ans = []
|
||||||
def traverse(node, ans)
|
def traverse(node, ans)
|
||||||
if node != nil
|
if node != nil
|
||||||
traverse(node.left, ans)
|
traverse(node.left, ans)
|
||||||
traverse(node.right,ans)
|
traverse(node.right, ans)
|
||||||
ans.push(node.val)
|
ans.push(node.val)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
traverse(root,ans)
|
end
|
||||||
return ans
|
traverse(root, ans)
|
||||||
|
return ans
|
||||||
end
|
end
|
|
@ -1,23 +1,23 @@
|
||||||
# Definition for a binary tree node.
|
# Definition for a binary tree node.
|
||||||
# class TreeNode
|
# class TreeNode
|
||||||
# attr_accessor :val, :left, :right
|
# attr_accessor :val, :left, :right
|
||||||
# def initialize(val)
|
# def initialize(val)
|
||||||
# @val = val
|
# @val = val
|
||||||
# @left, @right = nil, nil
|
# @left, @right = nil, nil
|
||||||
# end
|
# end
|
||||||
# end
|
# end
|
||||||
|
|
||||||
# @param {TreeNode} root
|
# @param {TreeNode} root
|
||||||
# @return {Integer[]}
|
# @return {Integer[]}
|
||||||
def preorder_traversal(root)
|
def preorder_traversal(root)
|
||||||
ans = []
|
ans = []
|
||||||
def traverse(node, ans)
|
def traverse(node, ans)
|
||||||
if node != nil
|
if node != nil
|
||||||
ans.push(node.val)
|
ans.push(node.val)
|
||||||
traverse(node.left, ans)
|
traverse(node.left, ans)
|
||||||
traverse(node.right,ans)
|
traverse(node.right, ans)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
traverse(root,ans)
|
end
|
||||||
return ans
|
traverse(root, ans)
|
||||||
|
return ans
|
||||||
end
|
end
|
Loading…
Reference in a new issue