Skip to content

Commit 7d7a8f5

Browse files
author
liwentian
committed
fd
1 parent d65a274 commit 7d7a8f5

File tree

7 files changed

+216
-9
lines changed

7 files changed

+216
-9
lines changed

ebook/tree/Tree.aux

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,19 @@
2626
\@writefile{toc}{\contentsline {section}{\numberline {1.7}Balanced Binary Tree}{7}{section.1.7}}
2727
\@writefile{toc}{\contentsline {subsubsection}{Description}{7}{section*.15}}
2828
\@writefile{toc}{\contentsline {subsubsection}{Solution}{7}{section*.16}}
29+
\@writefile{toc}{\contentsline {section}{\numberline {1.8}Binary Tree Maximum Path Sum}{8}{section.1.8}}
30+
\@writefile{toc}{\contentsline {subsubsection}{Description}{8}{section*.17}}
31+
\@writefile{toc}{\contentsline {subsubsection}{Solution}{8}{section*.18}}
32+
\@writefile{toc}{\contentsline {section}{\numberline {1.9}Populating Next Right Pointers in Each Node}{9}{section.1.9}}
33+
\@writefile{toc}{\contentsline {subsubsection}{Description}{9}{section*.19}}
34+
\@writefile{toc}{\contentsline {subsubsection}{Solution I}{10}{section*.20}}
35+
\@writefile{toc}{\contentsline {subsubsection}{Solution II}{10}{section*.21}}
36+
\@writefile{toc}{\contentsline {section}{\numberline {1.10}Convert Sorted Array to Binary Search Tree}{11}{section.1.10}}
37+
\@writefile{toc}{\contentsline {subsubsection}{Description}{11}{section*.22}}
38+
\@writefile{toc}{\contentsline {subsubsection}{Solution}{11}{section*.23}}
39+
\FN@pp@footnotehinttrue
2940
\@setckpt{Tree}{
30-
\setcounter{page}{8}
41+
\setcounter{page}{12}
3142
\setcounter{equation}{0}
3243
\setcounter{enumi}{0}
3344
\setcounter{enumii}{0}
@@ -37,20 +48,20 @@
3748
\setcounter{mpfootnote}{0}
3849
\setcounter{part}{0}
3950
\setcounter{chapter}{1}
40-
\setcounter{section}{7}
51+
\setcounter{section}{10}
4152
\setcounter{subsection}{0}
4253
\setcounter{subsubsection}{0}
4354
\setcounter{paragraph}{0}
4455
\setcounter{subparagraph}{0}
4556
\setcounter{figure}{0}
4657
\setcounter{table}{0}
47-
\setcounter{FancyVerbLine}{19}
58+
\setcounter{FancyVerbLine}{14}
4859
\setcounter{pp@next@reset}{1}
4960
\setcounter{@fnserial}{0}
5061
\setcounter{Item}{0}
5162
\setcounter{Hfootnote}{0}
5263
\setcounter{Hy@AnnotLevel}{0}
53-
\setcounter{bookmark@seq@number}{8}
64+
\setcounter{bookmark@seq@number}{11}
5465
\setcounter{parentequation}{0}
5566
\setcounter{section@level}{3}
5667
}

ebook/tree/Tree.tex

Lines changed: 184 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,4 +281,187 @@ \subsubsection{Solution}
281281
}
282282
\end{Code}
283283

284-
\newpage
284+
\newpage
285+
286+
\section{Binary Tree Maximum Path Sum} %%%%%%%%%%%%%%%%%%%%%%
287+
288+
\subsubsection{Description}
289+
290+
Given a binary tree, find the maximum path sum.
291+
292+
For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and does not need to go through the root.
293+
294+
For example:
295+
296+
Given the below binary tree,
297+
\begin{Code}
298+
1
299+
/ \
300+
2 3
301+
\end{Code}
302+
303+
Return 6.
304+
305+
\subsubsection{Solution}
306+
307+
\begin{Code}
308+
public int maxPathSum(TreeNode root) {
309+
return maxPathSum(root, null);
310+
}
311+
312+
/**
313+
* max表示包含root的单边路径最大和
314+
*/
315+
private int maxPathSum(TreeNode root, int[] max) {
316+
if (root == null) {
317+
return Integer.MIN_VALUE; // 此处容易错
318+
}
319+
int[] left = new int[1], right = new int[1];
320+
int leftMax = maxPathSum(root.left, left);
321+
int rightMax = maxPathSum(root.right, right);
322+
if (max != null) {
323+
max[0] = max(left[0], right[0], 0) + root.val; // 此处容易错
324+
}
325+
326+
// 容易错,要考虑到所有可能的情况
327+
return max(leftMax, rightMax, root.val, left[0] + right[0] + root.val,
328+
left[0] + root.val, right[0] + root.val);
329+
}
330+
331+
private int max(int... vals) {
332+
int max = Integer.MIN_VALUE;
333+
for (int val : vals) {
334+
max = Math.max(max, val);
335+
}
336+
return max;
337+
}
338+
\end{Code}
339+
340+
\newpage
341+
342+
\section{Populating Next Right Pointers in Each Node} %%%%%%%%%%%%%%%%%%%%%%
343+
344+
\subsubsection{Description}
345+
346+
Given a binary tree
347+
\begin{Code}
348+
struct TreeLinkNode {
349+
TreeLinkNode *left;
350+
TreeLinkNode *right;
351+
TreeLinkNode *next;
352+
}
353+
\end{Code}
354+
355+
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
356+
357+
Initially, all next pointers are set to NULL.
358+
359+
\textbf{Note:}
360+
361+
You may only use constant extra space.
362+
363+
You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
364+
365+
For example,
366+
367+
Given the following perfect binary tree,
368+
\begin{Code}
369+
1
370+
/ \
371+
2 3
372+
/ \ / \
373+
4 5 6 7
374+
\end{Code}
375+
376+
After calling your function, the tree should look like:
377+
\begin{Code}
378+
1 -> NULL
379+
/ \
380+
2 -> 3 -> NULL
381+
/ \ / \
382+
4->5->6->7 -> NULL
383+
\end{Code}
384+
385+
\newpage
386+
387+
\subsubsection{Solution I}
388+
389+
\begin{Code}
390+
/** 递归法,巧妙地运用dummy使代码很简洁
391+
* 假定当前root所在层已连好,要连下一层
392+
*/
393+
public void connect(TreeLinkNode root) {
394+
if (root == null) {
395+
return;
396+
}
397+
TreeLinkNode dummy = new TreeLinkNode(0), cur = dummy;
398+
for (TreeLinkNode p = root; p != null; p = p.next) {
399+
if (p.left != null) {
400+
cur.next = p.left;
401+
cur = cur.next;
402+
}
403+
if (p.right != null) {
404+
cur.next = p.right;
405+
cur = cur.next;
406+
}
407+
}
408+
connect(dummy.next);
409+
}
410+
411+
\end{Code}
412+
413+
\subsubsection{Solution II}
414+
415+
\begin{Code}
416+
/**
417+
* 将递归转成非递归很简单,就加一层循环,且结尾处加root = dummy.next即可
418+
*/
419+
public void connect2(TreeLinkNode root) {
420+
while (root != null) {
421+
TreeLinkNode dummy = new TreeLinkNode(0), cur = dummy;
422+
423+
for (TreeLinkNode p = root; p != null; p = p.next) {
424+
if (p.left != null) {
425+
cur.next = p.left;
426+
cur = cur.next;
427+
}
428+
if (p.right != null) {
429+
cur.next = p.right;
430+
cur = cur.next;
431+
}
432+
}
433+
434+
root = dummy.next;
435+
}
436+
}
437+
\end{Code}
438+
439+
\newpage
440+
441+
\section{Convert Sorted Array to Binary Search Tree} %%%%%%%%%%%%%%%%%%%%%%
442+
443+
\subsubsection{Description}
444+
445+
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
446+
447+
\subsubsection{Solution}
448+
449+
\begin{Code}
450+
public TreeNode sortedArrayToBST(int[] nums) {
451+
return sortedArrayToBST(nums, 0, nums.length);
452+
}
453+
454+
private TreeNode sortedArrayToBST(int[] nums, int start, int end) {
455+
if (start >= end) {
456+
return null;
457+
}
458+
int mid = start + (end - start) / 2;
459+
TreeNode root = new TreeNode(nums[mid]);
460+
root.left = sortedArrayToBST(nums, start, mid);
461+
root.right = sortedArrayToBST(nums, mid + 1, end);
462+
return root;
463+
}
464+
\end{Code}
465+
466+
\newpage
467+

ebook/tree/leetcode-tree.log

Lines changed: 4 additions & 4 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:44
1+
This is XeTeX, Version 3.14159265-2.6-0.99996 (TeX Live 2016) (preloaded format=xelatex 2017.9.4) 8 SEP 2017 22:06
22
entering extended mode
33
restricted \write18 enabled.
44
%&-line parsing enabled.
@@ -1894,7 +1894,7 @@ File: eu1lmtt.fd 2009/10/30 v1.6 Font defs for Latin Modern
18941894

18951895

18961896
] [2]
1897-
[3] [4] [5] [6] [7])
1897+
[3] [4] [5] [6] [7] [8] [9] [10]) [11]
18981898
No file leetcode-tree.ind.
18991899
Package atveryend Info: Empty hook `BeforeClearDocument' on input line 44.
19001900
Package atveryend Info: Empty hook `AfterLastShipout' on input line 44.
@@ -1906,10 +1906,10 @@ Package atveryend Info: Empty hook `AtVeryVeryEnd' on input line 44.
19061906
Here is how much of TeX's memory you used:
19071907
29168 strings out of 493591
19081908
553349 string characters out of 6143547
1909-
569007 words of memory out of 5000000
1909+
571061 words of memory out of 5000000
19101910
32211 multiletter control sequences out of 15000+600000
19111911
5522 words of font info for 54 fonts, out of 8000000 for 9000
19121912
1347 hyphenation exceptions out of 8191
19131913
65i,11n,77p,10419b,476s stack positions out of 5000i,500n,10000p,200000b,80000s
19141914

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

ebook/tree/leetcode-tree.out

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@
66
\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
77
\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
88
\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
9+
\BOOKMARK [1][-]{section.1.8}{\376\377\0001\000.\0008\000\040\000B\000i\000n\000a\000r\000y\000\040\000T\000r\000e\000e\000\040\000M\000a\000x\000i\000m\000u\000m\000\040\000P\000a\000t\000h\000\040\000S\000u\000m}{chapter.1}% 9
10+
\BOOKMARK [1][-]{section.1.9}{\376\377\0001\000.\0009\000\040\000P\000o\000p\000u\000l\000a\000t\000i\000n\000g\000\040\000N\000e\000x\000t\000\040\000R\000i\000g\000h\000t\000\040\000P\000o\000i\000n\000t\000e\000r\000s\000\040\000i\000n\000\040\000E\000a\000c\000h\000\040\000N\000o\000d\000e}{chapter.1}% 10
11+
\BOOKMARK [1][-]{section.1.10}{\376\377\0001\000.\0001\0000\000\040\000C\000o\000n\000v\000e\000r\000t\000\040\000S\000o\000r\000t\000e\000d\000\040\000A\000r\000r\000a\000y\000\040\000t\000o\000\040\000B\000i\000n\000a\000r\000y\000\040\000S\000e\000a\000r\000c\000h\000\040\000T\000r\000e\000e}{chapter.1}% 11

ebook/tree/leetcode-tree.pdf

24.2 KB
Binary file not shown.
10.9 KB
Binary file not shown.

ebook/tree/leetcode-tree.toc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,13 @@
2121
\contentsline {section}{\numberline {1.7}Balanced Binary Tree}{7}{section.1.7}
2222
\contentsline {subsubsection}{Description}{7}{section*.15}
2323
\contentsline {subsubsection}{Solution}{7}{section*.16}
24+
\contentsline {section}{\numberline {1.8}Binary Tree Maximum Path Sum}{8}{section.1.8}
25+
\contentsline {subsubsection}{Description}{8}{section*.17}
26+
\contentsline {subsubsection}{Solution}{8}{section*.18}
27+
\contentsline {section}{\numberline {1.9}Populating Next Right Pointers in Each Node}{9}{section.1.9}
28+
\contentsline {subsubsection}{Description}{9}{section*.19}
29+
\contentsline {subsubsection}{Solution I}{10}{section*.20}
30+
\contentsline {subsubsection}{Solution II}{10}{section*.21}
31+
\contentsline {section}{\numberline {1.10}Convert Sorted Array to Binary Search Tree}{11}{section.1.10}
32+
\contentsline {subsubsection}{Description}{11}{section*.22}
33+
\contentsline {subsubsection}{Solution}{11}{section*.23}

0 commit comments

Comments
 (0)