- Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path0094-binary-tree-inorder-traversal.rb
54 lines (47 loc) · 1.01 KB
/
0094-binary-tree-inorder-traversal.rb
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
# 94. Binary Tree Inorder Traversal
# Easy
# https://leetcode.com/problems/binary-tree-inorder-traversal
=begin
Given the root of a binary tree, return the inorder traversal of its nodes' values.
Example 1:
Input: root = [1,null,2,3]
Output: [1,3,2]
Example 2:
Input: root = []
Output: []
Example 3:
Input: root = [1]
Output: [1]
Constraints:
The number of nodes in the tree is in the range [0, 100].
-100 <= Node.val <= 100
=end
# frozen_string_literal: true
# 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 {Integer[]}
definorder_traversal(root)
ret=[]
stack=[root]
whiletrue
top=stack[-1]
if !top&.left.nil?
stack << top.left
else
t=stack.pop
t=stack.popift.nil?
breakift.nil?
ret << t.val
stack << t.right
end
end
ret
end