Skip to content

Addition of Symmetric Tree algorithm pseudocode #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Pseudocode/Trees/Symmetric Tree.pdf
Binary file not shown.
32 changes: 32 additions & 0 deletions Pseudocode/Trees/SymmetricTree.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {

//call the recurisve helper method passing root, root as the root of left and right subtrees initially
return helper(root, root);

}

//helper method that gets t1 and t2 as root of left and right subtrees
private boolean helper(TreeNode t1, TreeNode t2) {
//if t1 and t2 are both null then return true
if (t1 == null && t2 == null)
return true;

//if either t1 or t2 is null then return false
if (t1 != null && t2 != null)
//return t1.val == t2.val && helper(t1.left, t2.right) && helper(t1.right, t2.left)
return (t1.val == t2.val) && helper(t1.left, t2.right) && helper(t1.right, t2.left);

return false;
// T O(n) S O(n) due to recursion
}
}