Skip to content

Commit 7195871

Browse files
committed
feat: add algorithm description for symmetric tree
1 parent fa2e731 commit 7195871

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/easy/symmetric_tree.rs

+16
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ pub fn is_symmetric(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
5555

5656
}
5757

58+
/*
59+
Algorithm - BFS
60+
61+
1. Push root node twice into queue
62+
2. Pop two nodes from queue
63+
3. If both nodes are None, continue
64+
4. If one of the nodes is None, return false
65+
5. If both nodes are not None, check if their values are equal
66+
6. Push left child of first node and right child of second node into queue
67+
7. Push right child of first node and left child of second node into queue
68+
8. Repeat steps 2-7 until queue is empty
69+
70+
Time: O(n)
71+
Space: O(n)
72+
73+
*/
5874

5975
#[cfg(test)]
6076
mod tests {

0 commit comments

Comments
 (0)