
g0001_0100.s0094_binary_tree_inorder_traversal.Solution.dart Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
The newest version!
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Depth_First_Search #Tree #Binary_Tree
// #Stack #Data_Structure_I_Day_10_Tree #Udemy_Tree_Stack_Queue #Big_O_Time_O(n)_Space_O(n)
// #2024_10_06_Time_287_ms_(95.71%)_Space_151.5_MB_(11.43%)
/**
* Definition for a binary tree node.
* class TreeNode {
* int val;
* TreeNode? left;
* TreeNode? right;
* TreeNode([this.val = 0, this.left, this.right]);
* }
*/
class Stack {
final List _items = [];
void push(T item) {
_items.add(item);
}
T pop() {
return _items.removeLast();
}
T peek() {
return _items.last;
}
bool get isEmpty {
return _items.isEmpty;
}
int get size {
return _items.length;
}
}
class Solution {
List inorderTraversal(TreeNode? root) {
final result = List.empty(growable: true);
final stack = Stack();
while (root != null || !stack.isEmpty) {
while (root != null) {
stack.push(root);
root = root.left;
}
root = stack.pop();
result.add(root.val);
root = root.right;
}
return result;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy