剑指 Offer 28. 对称的二叉树
public boolean isSymmetric(TreeNode root) {
if (root == null) return false;
return isSymmetricHelper(root.left, root.right);
}
private boolean isSymmetricHelper(TreeNode left, TreeNode right) {
if (left == null && right == null) return true;
if (left == null && right != null || right == null && left != null) return false;
if (left.val != right.val) return false;
return isSymmetricHelper(left.left, right.right) && isSymmetricHelper(left.right, right.left);
}
public boolean isSymmetric(TreeNode root) {
if (root == null) return true;
return helper(root.left, root.right);
}
public boolean helper(TreeNode left, TreeNode right) {
Queue<TreeNode> leftQueue = new LinkedList<>();
Queue<TreeNode> rightQueue = new LinkedList<>();
leftQueue.offer(left);
rightQueue.offer(right);
while (!leftQueue.isEmpty()) {
TreeNode curLeft = leftQueue.poll();
TreeNode curRight = rightQueue.poll();
if (curLeft == null && curRight == null) {
continue;
}
if (curLeft != null && curRight == null || curLeft == null && curRight != null) {
return false;
}
if (curLeft.val != curRight.val) {
return false;
}
leftQueue.offer(curLeft.left);
rightQueue.offer(curRight.right);
leftQueue.offer(curLeft.right);
rightQueue.offer(curRight.left);
}
return true;
}
public boolean isSymmetric(TreeNode root) {
if (root == null) return true;
return helper(root.left, root.right);
}
public boolean helper(TreeNode left, TreeNode right) {
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(left);
queue.offer(right);
while (!queue.isEmpty()) {
TreeNode curLeft = queue.poll();
TreeNode curRight = queue.poll();
if (curLeft == null && curRight == null) {
continue;
}
if (curLeft != null && curRight == null || curLeft == null && curRight != null) {
return false;
}
if (curLeft.val != curRight.val) {
return false;
}
queue.offer(curLeft.left);
queue.offer(curRight.right);
queue.offer(curLeft.right);
queue.offer(curRight.left);
}
return true;
}
剑指 Offer 36. 二叉搜索树与双向链表
Node pre, head;
public Node treeToDoublyList(Node root) {
if (root == null) return null;
dfs(root);
head.left = pre;
pre.right = head;
return head;
}
public void dfs(Node cur) {
if (cur == null) return;
dfs(cur.left);
if (pre != null) pre.right = cur;
else head = cur;
cur.left = pre;
pre = cur;
dfs(cur.right);
}
Queue<Node> queue = new LinkedList<>();
public Node treeToDoublyList(Node root) {
if (root == null) return null;
put(root);
Node cur = queue.poll();
Node head = cur;
while (!queue.isEmpty()) {
Node next = queue.poll();
cur.right = next;
next.left = cur;
cur = next;
}
head.left = cur;
cur.right = head;
return head;
}
public void put(Node root) {
if (root == null) return;
put(root.left);
queue.offer(root);
put(root.right);
}
Queue<Node> queue = new LinkedList<>();
public Node treeToDoublyList(Node root) {
if (root == null) return null;
inorder(root);
Node cur = queue.poll();
Node head = cur;
while (!queue.isEmpty()) {
Node next = queue.poll();
cur.right = next;
next.left = cur;
cur = next;
}
head.left = cur;
cur.right = head;
return head;
}
public void inorder(Node root) {
Stack<Node> stack = new Stack<>();
while (root != null || !stack.isEmpty()) {
while (root != null) {
stack.push(root);
root = root.left;
}
if (!stack.isEmpty()) {
root = stack.pop();
queue.offer(root);
root = root.right;
}
}
}
剑指 Offer 68 - II. 二叉树的最近公共祖先、剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root == null) return null;
if (p == root || q == root) return root;
TreeNode pORq1 = lowestCommonAncestor(root.left, p, q);
TreeNode pORq2 = lowestCommonAncestor(root.right, p, q);
if (pORq1 != null && pORq2 != null) return root;
if (pORq1 == null && pORq2 != null) return pORq2;
if (pORq1 != null && pORq2 == null) return pORq1;
return null;
}
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
if (root.val > p.val && root.val > q.val) {
return lowestCommonAncestor(root.left, p, q);
} else if (root.val < p.val && root.val < q.val) {
return lowestCommonAncestor(root.right, p, q);
}
return root;
}
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
while (root != null) {
if (root.val > p.val && root.val > q.val) {
root = root.left;
} else if (root.val < p.val && root.val < q.val) {
root = root.right;
} else break;
}
return root;
}