From 7e0181c1b916024d3608c1baa48410bb6177051c Mon Sep 17 00:00:00 2001 From: CyC2018 <1029579233@qq.com> Date: Tue, 27 Feb 2018 15:29:46 +0800 Subject: [PATCH] auto commit --- notes/剑指 offer 题解.md | 23 ++++++++++++------ pics/6fec7f56-a685-4232-b03e-c92a8dfba486.png | Bin 0 -> 6144 bytes 2 files changed, 15 insertions(+), 8 deletions(-) create mode 100644 pics/6fec7f56-a685-4232-b03e-c92a8dfba486.png diff --git a/notes/剑指 offer 题解.md b/notes/剑指 offer 题解.md index dcd92668..156b86a5 100644 --- a/notes/剑指 offer 题解.md +++ b/notes/剑指 offer 题解.md @@ -254,16 +254,16 @@ public ArrayList printListFromTailToHead(ListNode listNode) { public TreeNode reConstructBinaryTree(int[] pre, int[] in) { return reConstructBinaryTree(pre, 0, pre.length - 1, in, 0, in.length - 1); } + private TreeNode reConstructBinaryTree(int[] pre, int preL, int preR, int[] in, int inL, int inR) { - if(preL > preR || inL > inR) return null; + if (preL == preR) return new TreeNode(pre[preL]); + if (preL > preR || inL > inR) return null; TreeNode root = new TreeNode(pre[preL]); - if (preL != preR) { - int idx = inL; - while (idx <= inR && in[idx] != root.val) idx++; - int leftTreeLen = idx - inL; - root.left = reConstructBinaryTree(pre, preL + 1, preL + leftTreeLen, in, inL, inL + leftTreeLen - 1); - root.right = reConstructBinaryTree(pre, preL + leftTreeLen + 1, preR, in, inL + leftTreeLen + 1, inR); - } + int midIdx = inL; + while (midIdx <= inR && in[midIdx] != root.val) midIdx++; + int leftTreeSize = midIdx - inL; + root.left = reConstructBinaryTree(pre, preL + 1, preL + leftTreeSize, in, inL, inL + leftTreeSize - 1); + root.right = reConstructBinaryTree(pre, preL + leftTreeSize + 1, preR, in, inL + leftTreeSize + 1, inR); return root; } ``` @@ -274,6 +274,13 @@ private TreeNode reConstructBinaryTree(int[] pre, int preL, int preR, int[] in, ¸ø¶¨Ò»¸ö¶þ²æÊ÷ºÍÆäÖеÄÒ»¸ö½áµã£¬ÇëÕÒ³öÖÐÐò±éÀú˳ÐòµÄÏÂÒ»¸ö½áµã²¢ÇÒ·µ»Ø¡£×¢Ò⣬Ê÷ÖеĽáµã²»½ö°üº¬×óÓÒ×Ó½áµã£¬Í¬Ê±°üº¬Ö¸Ïò¸¸½áµãµÄÖ¸Õë¡£ +**½âÌâ˼·** + +- Èç¹ûÒ»¸ö½ÚµãÓÐÓÒ×ÓÊ÷²»Îª¿Õ£¬ÄÇô¸Ã½ÚµãµÄÏÂÒ»¸ö½ÚµãÊÇÓÒ×ÓÊ÷µÄ×î×ó½Úµã£» +- ·ñÔò£¬ÏòÉÏÕÒµÚÒ»¸ö×óÁ´½ÓÖ¸ÏòµÄÊ÷°üº¬¸Ã½ÚµãµÄ×æÏȽڵ㡣 + +![](https://github.com/CyC2018/InterviewNotes/blob/master/pics/6fec7f56-a685-4232-b03e-c92a8dfba486.png) + ```java public TreeLinkNode GetNext(TreeLinkNode pNode) { if (pNode == null) return null; diff --git a/pics/6fec7f56-a685-4232-b03e-c92a8dfba486.png b/pics/6fec7f56-a685-4232-b03e-c92a8dfba486.png new file mode 100644 index 0000000000000000000000000000000000000000..b627cea5451dd448a0566a16ea377d65e1dc8efd GIT binary patch literal 6144 zcmZ{Ic|6qL_x~sqvL#!pL1R?5yoI6+V}vY2_I0#KvWyfGW|TdN8A%bMvZl!{+epMD zO_oTODTYCqW~^ggX1=4x9c_Oi|mprYu@U!UnTuRPy))~YB8N2E|?l`lew z5;$d6ROft6Nv0Gb-{Mi<5v%pXlx#ul${~V~y#R?~pK+J{W&P^(?=(_%)DtJ+`b!=I z5|{VmiKb8gs%^aalJ!&7!b;#?k14ZZU*B4F2jSd1z0totiL2XD1(Uh|?92pBCra}# zDDTYxG;S_e-tsRvhrGD!SvoLh@n6)n@LIFTABjP!!u;ztM-VL*9GMqF`7VQm$m=#I zeDid5{Bcl!zYX&vLx+=PNig4az=ym`NdCo@&Nlxa=93`h$o&i@Rtdk&;#$&hx@E-n z?h#e}z(ysv=I|xQ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