101.对称二叉树 java、python、c++的多种实现
This commit is contained in:
parent
16e17ce4ad
commit
5a5efc87a9
|
@ -10,6 +10,7 @@
|
|||
#include <numeric>
|
||||
#include <functional>
|
||||
#include <semaphore.h>
|
||||
#include <queue>
|
||||
#include "structure.h"
|
||||
|
||||
using namespace std;
|
||||
|
@ -93,17 +94,38 @@ public:
|
|||
* @see <a href="https://leetcode-cn.com/problems/symmetric-tree/">原题链接</a>
|
||||
*/
|
||||
static bool isSymmetric(TreeNode *root) {
|
||||
const auto &check = [](TreeNode *l, TreeNode *r) {
|
||||
if (root) {
|
||||
queue<TreeNode *> q;
|
||||
q.push(root->left);
|
||||
q.push(root->right);
|
||||
while (!q.empty()) {
|
||||
TreeNode *l = q.front();
|
||||
q.pop();
|
||||
TreeNode *r = q.front();
|
||||
q.pop();
|
||||
if (!l && !r) {
|
||||
continue;
|
||||
}
|
||||
if (!l || !r || l->val != r->val) {
|
||||
return false;
|
||||
}
|
||||
q.push(l->left);
|
||||
q.push(r->right);
|
||||
q.push(l->right);
|
||||
q.push(r->left);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
/*return !root || check(root->left, root->right);*/
|
||||
/*return !root || [](TreeNode *l, TreeNode *r) {
|
||||
const auto &s = [&](auto &&self, TreeNode *l, TreeNode *r) -> bool {
|
||||
return (!l && !r) ||
|
||||
(l && r) && (l->val == r->val) && self(self, l->left, r->right) && self(self, l->right, r->left);
|
||||
return (!l && !r) || (l && r) && (l->val == r->val) && self(self, l->left, r->right) && self(self, l->right, r->left);
|
||||
};
|
||||
return s(s, l, r);
|
||||
};
|
||||
return root == nullptr || check(root->left, root->right);
|
||||
/*return !root || (root->left && root->right) && (root->left->val == root->right->val) &&
|
||||
isSymmetric(new TreeNode{0, root->left->left, root->right->right}) &&
|
||||
isSymmetric(new TreeNode{0, root->left->right, root->right->left});*/
|
||||
}(root->left, root->right);*/
|
||||
/*return !root || (!root->left && !root->right) || (root->left && root->right) && (root->left->val == root->right->val) &&
|
||||
isSymmetric(new TreeNode(0, root->left->left, root->right->right)) &&
|
||||
isSymmetric(new TreeNode(0, root->left->right, root->right->left));*/
|
||||
}
|
||||
|
||||
static bool check(TreeNode *l, TreeNode *r) {
|
||||
|
|
|
@ -3,6 +3,15 @@ package com.iqiaoxu.code;
|
|||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// write your code here
|
||||
TreeNode n0 = new TreeNode(3);
|
||||
TreeNode n1 = new TreeNode(4);
|
||||
TreeNode n2 = new TreeNode(4);
|
||||
TreeNode n3 = new TreeNode(3);
|
||||
TreeNode n4 = new TreeNode(2, n0, n1);
|
||||
TreeNode n5 = new TreeNode(2, n2, n3);
|
||||
TreeNode n6 = new TreeNode(1, n4, n5);
|
||||
Solution solution = new Solution();
|
||||
boolean symmetric = solution.isSymmetric(n6);
|
||||
System.out.println(symmetric);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,10 +2,7 @@ package com.iqiaoxu.code;
|
|||
|
||||
import javafx.util.Pair;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
public class Solution {
|
||||
public int xorOperation(int n, int start) {
|
||||
|
@ -47,4 +44,44 @@ public class Solution {
|
|||
public int missingNumber(int[] nums) {
|
||||
return new int[]{nums.length, 1, nums.length + 1, 0}[nums.length & 3] ^ Arrays.stream(nums).reduce(0, (o, n) -> o ^ n);
|
||||
}
|
||||
|
||||
private boolean check(TreeNode l, TreeNode r) {
|
||||
return (l == null && r == null) || l != null && r != null && l.val == r.val && check(l.left, r.right) && check(l.right, r.left);
|
||||
}
|
||||
|
||||
/**
|
||||
* 101.对称二叉树
|
||||
*
|
||||
* @param root 二叉树
|
||||
* @return 是否是对称二叉树
|
||||
* @see <a href="https://leetcode-cn.com/problems/symmetric-tree/">原题链接</a>
|
||||
*/
|
||||
public boolean isSymmetric(TreeNode root) {
|
||||
/*if (root != null) {
|
||||
Queue<TreeNode> q = new LinkedList<>();
|
||||
q.offer(root.left);
|
||||
q.offer(root.right);
|
||||
while (!q.isEmpty()) {
|
||||
TreeNode l = q.poll();
|
||||
TreeNode r = q.poll();
|
||||
if (l == null && r == null) {
|
||||
continue;
|
||||
}
|
||||
if (l == null || r == null || l.val != r.val) {
|
||||
return false;
|
||||
}
|
||||
q.offer(l.left);
|
||||
q.offer(r.right);
|
||||
q.offer(l.right);
|
||||
q.offer(r.left);
|
||||
}
|
||||
}
|
||||
return true;*/
|
||||
/*return root == null || check(root.left, root.right);*/
|
||||
return root == null ||
|
||||
(root.left == null && root.right == null) ||
|
||||
!(root.left == null || root.right == null || root.left.val != root.right.val) &&
|
||||
isSymmetric(new TreeNode(0, root.left.left, root.right.right)) &&
|
||||
isSymmetric(new TreeNode(0, root.left.right, root.right.left));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
package com.iqiaoxu.code;
|
||||
|
||||
public class TreeNode {
|
||||
int val;
|
||||
TreeNode left;
|
||||
TreeNode right;
|
||||
TreeNode() {}
|
||||
TreeNode(int val) { this.val = val; }
|
||||
TreeNode(int val, TreeNode left, TreeNode right) {
|
||||
this.val = val;
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
}
|
|
@ -2,6 +2,13 @@ import functools
|
|||
from typing import List
|
||||
|
||||
|
||||
class TreeNode:
|
||||
def __init__(self, val=0, left=None, right=None):
|
||||
self.val = val
|
||||
self.left = left
|
||||
self.right = right
|
||||
|
||||
|
||||
class Solution:
|
||||
def xorOperation(self, n: int, start: int) -> int:
|
||||
# from functools import reduce
|
||||
|
@ -53,3 +60,31 @@ class Solution:
|
|||
f = f * 0.5 + x_half / f
|
||||
f = f * 0.5 + x_half / f
|
||||
return int(f)
|
||||
|
||||
def isSymmetric(self, root: TreeNode) -> bool:
|
||||
"""
|
||||
101.对称二叉树
|
||||
:param root: 二叉树
|
||||
:return: 是否是对称二叉树
|
||||
"""
|
||||
|
||||
"""def check(l: TreeNode, r: TreeNode) -> bool:
|
||||
return (l is None and r is None) or (l is not None and r is not None) and (l.val == r.val) and check(l.left, r.right) and check(l.right, r.left)
|
||||
|
||||
return root is None or check(root.left, root.right)"""
|
||||
"""if root is not None:
|
||||
q = [(root.left, root.right)]
|
||||
while len(q) > 0:
|
||||
l, r = q.pop()
|
||||
if l is None and r is None:
|
||||
continue
|
||||
if l is None or r is None or l.val != r.val:
|
||||
return False
|
||||
q.append((l.left, r.right))
|
||||
q.append((l.right, r.left))
|
||||
return True"""
|
||||
return root is None or \
|
||||
(root.left is None and root.right is None) or \
|
||||
not (root.left is None or root.right is None or root.left.val != root.right.val) and \
|
||||
self.isSymmetric(TreeNode(0, root.left.left, root.right.right)) and \
|
||||
self.isSymmetric(TreeNode(0, root.left.right, root.right.left))
|
||||
|
|
Loading…
Reference in New Issue