Skip to content
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

leetcode 101. 对称二叉树 #树 #25

Open
webVueBlog opened this issue May 28, 2022 · 1 comment
Open

leetcode 101. 对称二叉树 #树 #25

webVueBlog opened this issue May 28, 2022 · 1 comment

Comments

@webVueBlog
Copy link
Member

image

101. 对称二叉树

@webVueBlog
Copy link
Member Author

101. 对称二叉树

/*
 * @lc app=leetcode.cn id=101 lang=javascript
 *
 * [101] 对称二叉树
 */

// @lc code=start
/**
 * 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}
进阶:你可以运用递归和迭代两种方法解决这个问题吗?
(64 ms)
 */
// 迭代
var isSymmetric = function(root) {
 let queue = [root, root];
 while(queue.length > 0) {
  let r1 = queue.shift();
  let r2 = queue.shift();
  if(!r1 && !r2) continue;
  if(!r1 || !r2) return false;
  if(r1.val !== r2.val) return false;
  queue.push(r1.left)
  queue.push(r2.right)
  queue.push(r1.right)
  queue.push(r2.left);
 }
 return true
}

//递归
// var isSymmetric = function(root) {
//  function isMirror(r1, r2) {
//   if(!r1 && !r2) return true
//   if(!r1 || !r2) return false
//   return r1.val === r2.val && isMirror(r1.left, r2.right) && isMirror(r1.right, r2.left)
//  }
//  return isMirror(root, root)
// }

//  var isSymmetric = function(root) {
//   if (!root) return true;
  
//   return symmetryChecker(root.left, root.right);
// };

// function symmetryChecker(left, right) {
//   if (left == null && right == null) return true;
//   if (left == null || right == null) return false;
//   if (left.val !== right.val) return false;
  
//   return symmetryChecker(left.left, right.right) && symmetryChecker(left.right, right.left);
// }
// @lc code=end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant