Skip to content

Commit d65a274

Browse files
author
liwentian
committed
fd
1 parent ac64623 commit d65a274

File tree

7 files changed

+252
-14
lines changed

7 files changed

+252
-14
lines changed

ebook/tree/Tree.aux

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,23 @@
1111
\@writefile{toc}{\contentsline {subsubsection}{Description}{2}{section*.4}}
1212
\@writefile{toc}{\contentsline {subsubsection}{Solution I}{2}{section*.5}}
1313
\@writefile{toc}{\contentsline {subsubsection}{Solution II}{2}{section*.6}}
14-
\FN@pp@footnotehinttrue
14+
\@writefile{toc}{\contentsline {section}{\numberline {1.3}Same Tree}{3}{section.1.3}}
15+
\@writefile{toc}{\contentsline {subsubsection}{Description}{3}{section*.7}}
16+
\@writefile{toc}{\contentsline {subsubsection}{Solution}{3}{section*.8}}
17+
\@writefile{toc}{\contentsline {section}{\numberline {1.4}Binary Search Tree Iterator}{4}{section.1.4}}
18+
\@writefile{toc}{\contentsline {subsubsection}{Description}{4}{section*.9}}
19+
\@writefile{toc}{\contentsline {subsubsection}{Solution}{4}{section*.10}}
20+
\@writefile{toc}{\contentsline {section}{\numberline {1.5}Unique Binary Search Trees}{5}{section.1.5}}
21+
\@writefile{toc}{\contentsline {subsubsection}{Description}{5}{section*.11}}
22+
\@writefile{toc}{\contentsline {subsubsection}{Solution}{5}{section*.12}}
23+
\@writefile{toc}{\contentsline {section}{\numberline {1.6}Lowest Common Ancestor of a Binary Search Tree}{6}{section.1.6}}
24+
\@writefile{toc}{\contentsline {subsubsection}{Description}{6}{section*.13}}
25+
\@writefile{toc}{\contentsline {subsubsection}{Solution}{6}{section*.14}}
26+
\@writefile{toc}{\contentsline {section}{\numberline {1.7}Balanced Binary Tree}{7}{section.1.7}}
27+
\@writefile{toc}{\contentsline {subsubsection}{Description}{7}{section*.15}}
28+
\@writefile{toc}{\contentsline {subsubsection}{Solution}{7}{section*.16}}
1529
\@setckpt{Tree}{
16-
\setcounter{page}{3}
30+
\setcounter{page}{8}
1731
\setcounter{equation}{0}
1832
\setcounter{enumi}{0}
1933
\setcounter{enumii}{0}
@@ -23,20 +37,20 @@
2337
\setcounter{mpfootnote}{0}
2438
\setcounter{part}{0}
2539
\setcounter{chapter}{1}
26-
\setcounter{section}{2}
40+
\setcounter{section}{7}
2741
\setcounter{subsection}{0}
2842
\setcounter{subsubsection}{0}
2943
\setcounter{paragraph}{0}
3044
\setcounter{subparagraph}{0}
3145
\setcounter{figure}{0}
3246
\setcounter{table}{0}
33-
\setcounter{FancyVerbLine}{26}
47+
\setcounter{FancyVerbLine}{19}
3448
\setcounter{pp@next@reset}{1}
3549
\setcounter{@fnserial}{0}
3650
\setcounter{Item}{0}
3751
\setcounter{Hfootnote}{0}
3852
\setcounter{Hy@AnnotLevel}{0}
39-
\setcounter{bookmark@seq@number}{3}
53+
\setcounter{bookmark@seq@number}{8}
4054
\setcounter{parentequation}{0}
4155
\setcounter{section@level}{3}
4256
}

ebook/tree/Tree.tex

Lines changed: 204 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,207 @@ \subsubsection{Solution II}
7878

7979
return root;
8080
}
81-
\end{Code}
81+
\end{Code}
82+
83+
\newpage
84+
85+
\section{Same Tree} %%%%%%%%%%%%%%%%%%%%%%
86+
87+
\subsubsection{Description}
88+
89+
Given two binary trees, write a function to check if they are equal or not.
90+
91+
Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
92+
93+
\subsubsection{Solution}
94+
95+
\begin{Code}
96+
public boolean isSameTree(TreeNode p, TreeNode q) {
97+
if (p == null && q == null) {
98+
return true;
99+
}
100+
if (p == null || q == null) {
101+
return false;
102+
}
103+
return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
104+
}
105+
\end{Code}
106+
107+
\newpage
108+
109+
\section{Binary Search Tree Iterator} %%%%%%%%%%%%%%%%%%%%%%
110+
111+
\subsubsection{Description}
112+
113+
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.
114+
115+
Calling next() will return the next smallest number in the BST.
116+
117+
\textbf{Note:} next() and hasNext() should run in average O(1) time and uses O(h) memory, where h is the height of the tree.
118+
119+
\subsubsection{Solution}
120+
121+
\begin{Code}
122+
public class BSTIterator {
123+
124+
private Stack<TreeNode> mStack;
125+
private TreeNode mCurNode;
126+
127+
public BSTIterator(TreeNode root) {
128+
mStack = new Stack<TreeNode>();
129+
mCurNode = root;
130+
}
131+
132+
public boolean hasNext() {
133+
return !mStack.isEmpty() || mCurNode != null;
134+
}
135+
136+
public int next() {
137+
int result = -1;
138+
139+
while (hasNext()) {
140+
if (mCurNode != null) {
141+
mStack.push(mCurNode);
142+
mCurNode = mCurNode.left;
143+
} else {
144+
mCurNode = mStack.pop();
145+
result = mCurNode.val;
146+
mCurNode = mCurNode.right;
147+
break;
148+
}
149+
}
150+
151+
return result;
152+
}
153+
}
154+
\end{Code}
155+
156+
\newpage
157+
158+
\section{Unique Binary Search Trees} %%%%%%%%%%%%%%%%%%%%%%
159+
160+
\subsubsection{Description}
161+
162+
Given n, how many structurally unique BST's (binary search trees) that store values 1...n?
163+
164+
For example,
165+
166+
Given n = 3, there are a total of 5 unique BST's.
167+
\begin{Code}
168+
1 3 3 2 1
169+
\ / / / \ \
170+
3 2 1 1 3 2
171+
/ / \ \
172+
2 1 2 3
173+
\end{Code}
174+
175+
\subsubsection{Solution}
176+
\begin{Code}
177+
public int numTrees(int n) {
178+
int[] dp = new int[n + 1];
179+
dp[0] = 1;
180+
181+
for (int i = 1; i <= n; i++) {
182+
for (int j = 0; j < i; j++) {
183+
dp[i] += dp[j] * dp[i - j - 1];
184+
}
185+
}
186+
187+
return dp[n];
188+
}
189+
\end{Code}
190+
191+
\newpage
192+
193+
\section{Lowest Common Ancestor of a Binary Search Tree} %%%%%%%%%%%%%%%%%%%%%%
194+
195+
\subsubsection{Description}
196+
197+
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.
198+
199+
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”
200+
\begin{Code}
201+
_______6______
202+
/ \
203+
___2__ ___8__
204+
/ \ / \
205+
0 _4 7 9
206+
/ \
207+
3 5
208+
\end{Code}
209+
For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6. Another example is LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.
210+
211+
\subsubsection{Solution}
212+
213+
\begin{Code}
214+
// 耗时9ms
215+
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
216+
if (!checkExist(root, p) || !checkExist(root, q)) {
217+
throw new IllegalArgumentException("Not exist!!");
218+
}
219+
220+
while (root != null) {
221+
if (p.val < root.val && q.val < root.val) {
222+
root = root.left;
223+
} else if (p.val > root.val && q.val > root.val) {
224+
root = root.right;
225+
} else {
226+
break;
227+
}
228+
}
229+
return root;
230+
}
231+
232+
/**
233+
* 如何判断p或q一定存在,如果是BST就很简单
234+
*/
235+
private boolean checkExist(TreeNode root, TreeNode node) {
236+
TreeNode cur = root;
237+
while (cur != null) {
238+
if (node.val > cur.val) {
239+
cur = cur.right;
240+
} else if (node.val < cur.val) {
241+
cur = cur.left;
242+
} else {
243+
return true;
244+
}
245+
}
246+
return false;
247+
}
248+
\end{Code}
249+
250+
\newpage
251+
252+
\section{Balanced Binary Tree} %%%%%%%%%%%%%%%%%%%%%%
253+
254+
\subsubsection{Description}
255+
256+
Given a binary tree, determine if it is height-balanced.
257+
258+
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
259+
260+
\subsubsection{Solution}
261+
262+
\begin{Code}
263+
public boolean isBalanced(TreeNode root) {
264+
return isBalanced(root, null);
265+
}
266+
267+
private boolean isBalanced(TreeNode root, int[] height) {
268+
if (root == null) {
269+
return true;
270+
}
271+
272+
int[] left = new int[1], right = new int[1];
273+
274+
boolean result = isBalanced(root.left, left) && isBalanced(root.right, right);
275+
276+
if (height != null) {
277+
height[0] = Math.max(left[0], right[0]) + 1;
278+
}
279+
280+
return result && Math.abs(left[0] - right[0]) <= 1;
281+
}
282+
\end{Code}
283+
284+
\newpage

ebook/tree/leetcode-tree.log

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
This is XeTeX, Version 3.14159265-2.6-0.99996 (TeX Live 2016) (preloaded format=xelatex 2017.9.4) 8 SEP 2017 08:09
1+
This is XeTeX, Version 3.14159265-2.6-0.99996 (TeX Live 2016) (preloaded format=xelatex 2017.9.4) 8 SEP 2017 08:44
22
entering extended mode
33
restricted \write18 enabled.
44
%&-line parsing enabled.
@@ -1893,7 +1893,8 @@ File: eu1lmtt.fd 2009/10/30 v1.6 Font defs for Latin Modern
18931893
[1
18941894

18951895

1896-
]) [2]
1896+
] [2]
1897+
[3] [4] [5] [6] [7])
18971898
No file leetcode-tree.ind.
18981899
Package atveryend Info: Empty hook `BeforeClearDocument' on input line 44.
18991900
Package atveryend Info: Empty hook `AfterLastShipout' on input line 44.
@@ -1903,12 +1904,12 @@ Package atveryend Info: Empty hook `AtEndAfterFileList' on input line 44.
19031904
Package atveryend Info: Empty hook `AtVeryVeryEnd' on input line 44.
19041905
)
19051906
Here is how much of TeX's memory you used:
1906-
29132 strings out of 493591
1907-
550886 string characters out of 6143547
1908-
566595 words of memory out of 5000000
1909-
32177 multiletter control sequences out of 15000+600000
1910-
5506 words of font info for 52 fonts, out of 8000000 for 9000
1907+
29168 strings out of 493591
1908+
553349 string characters out of 6143547
1909+
569007 words of memory out of 5000000
1910+
32211 multiletter control sequences out of 15000+600000
1911+
5522 words of font info for 54 fonts, out of 8000000 for 9000
19111912
1347 hyphenation exceptions out of 8191
19121913
65i,11n,77p,10419b,476s stack positions out of 5000i,500n,10000p,200000b,80000s
19131914

1914-
Output written on leetcode-tree.pdf (4 pages).
1915+
Output written on leetcode-tree.pdf (9 pages).

ebook/tree/leetcode-tree.out

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
\BOOKMARK [0][-]{chapter.1}{\376\377\173\054\0001\172\340\000\040\000\040\000T\000r\000e\000e}{}% 1
22
\BOOKMARK [1][-]{section.1.1}{\376\377\0001\000.\0001\000\040\000M\000a\000x\000i\000m\000u\000m\000\040\000D\000e\000p\000t\000h\000\040\000o\000f\000\040\000B\000i\000n\000a\000r\000y\000\040\000T\000r\000e\000e}{chapter.1}% 2
33
\BOOKMARK [1][-]{section.1.2}{\376\377\0001\000.\0002\000\040\000I\000n\000v\000e\000r\000t\000\040\000B\000i\000n\000a\000r\000y\000\040\000T\000r\000e\000e}{chapter.1}% 3
4+
\BOOKMARK [1][-]{section.1.3}{\376\377\0001\000.\0003\000\040\000S\000a\000m\000e\000\040\000T\000r\000e\000e}{chapter.1}% 4
5+
\BOOKMARK [1][-]{section.1.4}{\376\377\0001\000.\0004\000\040\000B\000i\000n\000a\000r\000y\000\040\000S\000e\000a\000r\000c\000h\000\040\000T\000r\000e\000e\000\040\000I\000t\000e\000r\000a\000t\000o\000r}{chapter.1}% 5
6+
\BOOKMARK [1][-]{section.1.5}{\376\377\0001\000.\0005\000\040\000U\000n\000i\000q\000u\000e\000\040\000B\000i\000n\000a\000r\000y\000\040\000S\000e\000a\000r\000c\000h\000\040\000T\000r\000e\000e\000s}{chapter.1}% 6
7+
\BOOKMARK [1][-]{section.1.6}{\376\377\0001\000.\0006\000\040\000L\000o\000w\000e\000s\000t\000\040\000C\000o\000m\000m\000o\000n\000\040\000A\000n\000c\000e\000s\000t\000o\000r\000\040\000o\000f\000\040\000a\000\040\000B\000i\000n\000a\000r\000y\000\040\000S\000e\000a\000r\000c\000h\000\040\000T\000r\000e\000e}{chapter.1}% 7
8+
\BOOKMARK [1][-]{section.1.7}{\376\377\0001\000.\0007\000\040\000B\000a\000l\000a\000n\000c\000e\000d\000\040\000B\000i\000n\000a\000r\000y\000\040\000T\000r\000e\000e}{chapter.1}% 8

ebook/tree/leetcode-tree.pdf

26.1 KB
Binary file not shown.
14 KB
Binary file not shown.

ebook/tree/leetcode-tree.toc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,18 @@
66
\contentsline {subsubsection}{Description}{2}{section*.4}
77
\contentsline {subsubsection}{Solution I}{2}{section*.5}
88
\contentsline {subsubsection}{Solution II}{2}{section*.6}
9+
\contentsline {section}{\numberline {1.3}Same Tree}{3}{section.1.3}
10+
\contentsline {subsubsection}{Description}{3}{section*.7}
11+
\contentsline {subsubsection}{Solution}{3}{section*.8}
12+
\contentsline {section}{\numberline {1.4}Binary Search Tree Iterator}{4}{section.1.4}
13+
\contentsline {subsubsection}{Description}{4}{section*.9}
14+
\contentsline {subsubsection}{Solution}{4}{section*.10}
15+
\contentsline {section}{\numberline {1.5}Unique Binary Search Trees}{5}{section.1.5}
16+
\contentsline {subsubsection}{Description}{5}{section*.11}
17+
\contentsline {subsubsection}{Solution}{5}{section*.12}
18+
\contentsline {section}{\numberline {1.6}Lowest Common Ancestor of a Binary Search Tree}{6}{section.1.6}
19+
\contentsline {subsubsection}{Description}{6}{section*.13}
20+
\contentsline {subsubsection}{Solution}{6}{section*.14}
21+
\contentsline {section}{\numberline {1.7}Balanced Binary Tree}{7}{section.1.7}
22+
\contentsline {subsubsection}{Description}{7}{section*.15}
23+
\contentsline {subsubsection}{Solution}{7}{section*.16}

0 commit comments

Comments
 (0)