X Tutup
Skip to content

Commit 99e625f

Browse files
author
liwentian
committed
fd
1 parent bec0d05 commit 99e625f

File tree

2 files changed

+2
-62
lines changed

2 files changed

+2
-62
lines changed

ebook/tree/Tree.tex

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -409,66 +409,6 @@ \subsubsection{Solution II}
409409

410410
\newpage
411411

412-
\section{Populating Next Right Pointers in Each Node II} %%%%%%%%%%%%%%%%%%%%%%


413-
414-
\subsubsection{Description}
415-
Follow up for problem "Populating Next Right Pointers in Each Node".
416-
417-
What if the given tree could be any binary tree? Would your previous solution still work?
418-
419-
\textbf{Note:}
420-
421-
You may only use constant extra space.
422-
423-
\subsubsection{Solution I}
424-
425-
\begin{Code}
426-
/**
427-
* 和上题的区别在于这里root下一个循环要指向dummy.next,即下一层链表表头
428-
*/
429-
public void connect(TreeLinkNode root) {
430-
while (root != null) {
431-
TreeLinkNode dummy = new TreeLinkNode(0), cur = dummy;
432-
for (TreeLinkNode node = root; node != null; node = node.next) {
433-
if (node.left != null) {
434-
cur.next = node.left;
435-
cur = cur.next;
436-
}
437-
438-
if (node.right != null) {
439-
cur.next = node.right;
440-
cur = cur.next;
441-
}
442-
}
443-
root = dummy.next;
444-
}
445-
}
446-
\end{Code}
447-
448-
\subsubsection{Solution II}
449-
\begin{Code}
450-
/** 递归,和上题相比改都不用改
451-
public void connect(TreeLinkNode root) {
452-
if (root == null) {
453-
return;
454-
}
455-
TreeLinkNode dummy = new TreeLinkNode(0), cur = dummy;
456-
for (TreeLinkNode p = root; p != null; p = p.next) {
457-
if (p.left != null) {
458-
cur.next = p.left;
459-
cur = cur.next;
460-
}
461-
if (p.right != null) {
462-
cur.next = p.right;
463-
cur = cur.next;
464-
}
465-
}
466-
connect(dummy.next);
467-
}*/
468-
\end{Code}
469-
470-
\newpage
471-
472412
\section{Convert Sorted Array to Binary Search Tree} %%%%%%%%%%%%%%%%%%%%%%
473413

474414
\subsubsection{Description}
@@ -642,7 +582,7 @@ \subsubsection{Solution II}
642582
}
643583

644584
for (TreeNode node = q; node != null; node = parents.get(node)) {
645-
if (!set.contains(node)) {
585+
if (set.contains(node)) {
646586
return node;
647587
}
648588
}

solution/src/main/java/com/inuker/solution/LowestCommonAncestorOfBinaryTree.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public TreeNode lowestCommonAncestor2(TreeNode root, TreeNode p, TreeNode q) {
8080
}
8181

8282
for (TreeNode node = q; node != null; node = parents.get(node)) {
83-
if (!set.contains(node)) {
83+
if (set.contains(node)) {
8484
return node;
8585
}
8686
}

0 commit comments

Comments
 (0)
X Tutup