-
Notifications
You must be signed in to change notification settings - Fork 1
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
101. Symmetric Tree #188
Comments
Solution 1 ( DFS 递归 )
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isSymmetric = function(root) {
if (root === null) {
return true;
}
return isSymmetricEqual(root.left, root.right);
};
function isSymmetricEqual(treeNode1, treeNode2) {
if (treeNode1 === null && treeNode2 === null) {
return true;
}
if (treeNode1 === null || treeNode2 === null) {
return false;
}
return treeNode1.val === treeNode2.val
&& isSymmetricEqual(treeNode1.left, treeNode2.right)
&& isSymmetricEqual(treeNode1.right, treeNode2.left);
}
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
var isSymmetric = function(root: TreeNode | null): boolean {
if (root === null) {
return true;
}
return isSymmetricEqual(root.left, root.right);
};
function isSymmetricEqual(treeNode1: TreeNode | null, treeNode2: TreeNode | null): boolean {
if (treeNode1 === null && treeNode2 === null) {
return true;
}
if (treeNode1 === null || treeNode2 === null) {
return false;
}
return treeNode1.val === treeNode2.val
&& isSymmetricEqual(treeNode1.left, treeNode2.right)
&& isSymmetricEqual(treeNode1.right, treeNode2.left);
} Solution 2 ( DFS 迭代 )
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isSymmetric = function(root) {
if (root === null) {
return true;
}
let stack = [[root.left, root.right]];
while(stack.length !== 0) {
const [treeNode1, treeNode2] = stack.pop();
if (treeNode1 === null && treeNode2 === null) {
continue;
}
if (treeNode1 === null || treeNode2 === null) {
return false;
}
if (treeNode1.val !== treeNode2.val) {
return false;
}
stack.push([treeNode1.left, treeNode2.right], [treeNode1.right, treeNode2.left]);
}
return true;
};
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
var isSymmetric = function(root: TreeNode | null): boolean {
if (root === null) {
return true;
}
let stack: [TreeNode | null, TreeNode | null][] = [[root.left, root.right]];
while(stack.length !== 0) {
const [treeNode1, treeNode2] = stack.pop() as [TreeNode | null, TreeNode | null];
if (treeNode1 === null && treeNode2 === null) {
continue;
}
if (treeNode1 === null || treeNode2 === null) {
return false;
}
if (treeNode1.val !== treeNode2.val) {
return false;
}
stack.push([treeNode1.left, treeNode2.right], [treeNode1.right, treeNode2.left]);
}
return true;
}; Solution 3 ( BFS )
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isSymmetric = function(root) {
if (root === null) {
return true;
}
let queue = [[root.left, root.right]];
while(queue.length !== 0) {
const [treeNode1, treeNode2] = queue.shift();
if (treeNode1 === null && treeNode2 === null) {
continue;
}
if (treeNode1 === null || treeNode2 === null) {
return false;
}
if (treeNode1.val !== treeNode2.val) {
return false;
}
queue.push([treeNode1.left, treeNode2.right], [treeNode1.right, treeNode2.left]);
}
return true;
};
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isSymmetric = function(root) {
if (root === null) {
return true;
}
let queue = [[root.left, root.right]];
while(queue.length !== 0) {
const [treeNode1, treeNode2] = queue.shift();
if (treeNode1 === null && treeNode2 === null) {
continue;
}
if (treeNode1 === null || treeNode2 === null) {
return false;
}
if (treeNode1.val !== treeNode2.val) {
return false;
}
queue.push([treeNode1.left, treeNode2.right], [treeNode1.right, treeNode2.left]);
}
return true;
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
101. Symmetric Tree
给定一个二叉树,检查它是否是镜像对称的。
例如,二叉树 [1,2,2,3,4,4,3] 是对称的
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的
Follow up:
The text was updated successfully, but these errors were encountered: