日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[Leetcode][第106题][JAVA][ 从中序与后序遍历序列构造二叉树][分治][递归]

發布時間:2023/12/10 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [Leetcode][第106题][JAVA][ 从中序与后序遍历序列构造二叉树][分治][递归] 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

【問題描述】[中等]

【解答思路】



public class Solution {public TreeNode buildTree(int[] inorder, int[] postorder) {int inLen = inorder.length;int postLen = postorder.length;// 特判if (inLen != postLen) {throw new RuntimeException("輸入錯誤");}return buildTree(inorder, 0, inLen - 1, postorder, 0, postLen - 1);}/*** 使用中序遍歷序列 inorder 的子區間 [inLeft, inRight]* 與后序遍歷序列 postorder 的子區間 [postLeft, postRight] 構建二叉樹** @param inorder 中序遍歷序列* @param inLeft 中序遍歷序列的左邊界* @param inRight 中序遍歷序列的右邊界* @param postorder 后序遍歷序列* @param postLeft 后序遍歷序列的左邊界* @param postRight 后序遍歷序列的右邊界* @return 二叉樹的根結點*/private TreeNode buildTree(int[] inorder, int inLeft, int inRight,int[] postorder, int postLeft, int postRight) {if (inLeft > inRight || postLeft > postRight) {return null;}int pivot = postorder[postRight];int pivotIndex = inLeft;// 注意這里如果編寫不當,有數組下標越界的風險while (inorder[pivotIndex] != pivot) {pivotIndex++;}TreeNode root = new TreeNode(pivot);root.left = buildTree(inorder, inLeft, pivotIndex - 1,postorder, postLeft, postRight - inRight + pivotIndex - 1);root.right = buildTree(inorder, pivotIndex + 1, inRight,postorder, postRight - inRight + pivotIndex, postRight - 1);return root;} }作者:liweiwei1419 鏈接:https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/solution/hou-xu-bian-li-python-dai-ma-java-dai-ma-by-liwe-2/

HashMap優化



class Solution {HashMap<Integer,Integer> memo = new HashMap<>();int[] post;public TreeNode buildTree(int[] inorder, int[] postorder) {for(int i = 0;i < inorder.length; i++) memo.put(inorder[i], i);post = postorder;TreeNode root = buildTree(0, inorder.length - 1, 0, post.length - 1);return root;}public TreeNode buildTree(int is, int ie, int ps, int pe) {if(ie < is || pe < ps) return null;int root = post[pe];int ri = memo.get(root);TreeNode node = new TreeNode(root);node.left = buildTree(is, ri - 1, ps, ps + ri - is - 1);node.right = buildTree(ri + 1, ie, ps + ri - is, pe - 1);return node;} }作者:reals 鏈接:https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/solution/tu-jie-gou-zao-er-cha-shu-wei-wan-dai-xu-by-user72/

copyOfRange 左必右開

class Solution {public TreeNode buildTree(int[] inorder, int[] postorder) {if(inorder==null || postorder==null) {return null;}return helper(inorder,postorder);}private TreeNode helper(int[] in, int[] post) {if(in.length==0) {return null;}//根據后序數組的最后一個元素,創建根節點TreeNode root = new TreeNode(post[post.length-1]);//在中序數組中查找值等于【后序數組最后一個元素】的下標for(int i=0;i<in.length;++i) {if(in[i]==post[post.length-1]) {//確定這個下標i后,將中序數組分成兩部分,后序數組分成兩部分int[] inLeft = Arrays.copyOfRange(in,0,i);int[] inRight = Arrays.copyOfRange(in,i+1,in.length);int[] postLeft = Arrays.copyOfRange(post,0,i);int[] postRight = Arrays.copyOfRange(post,i,post.length-1);//遞歸處理中序數組左邊,后序數組左邊root.left = helper(inLeft,postLeft);//遞歸處理中序數組右邊,后序數組右邊root.right = helper(inRight,postRight);break;}}return root;} }作者:wang_ni_ma 鏈接:https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/solution/liang-chong-shi-xian-dong-hua-yan-shi-106-cong-zho/ 來源:力扣(LeetCode) 著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。

【總結】

1.前中后序遍歷變化的是[中]的位置,左到右的順序不改變
  • 前序遍歷 中左右
  • 中序遍歷 左中右
  • 后續遍歷 左右中
2.還原二叉樹 借助HashMap or copyOfRange

根據前序和后序遍歷構造二叉樹
[Leetcode][第889題][JAVA][根據前序和后序遍歷構造二叉樹][分治][遞歸]
前序+中序遍歷可畫出原二叉樹
[Leedcode][JAVA][第105題][從前序與中序遍歷序列構造二叉樹][棧][遞歸][二叉樹]
后續+中序遍歷可畫出原二叉樹
[Leetcode][第106題][JAVA][ 從中序與后序遍歷序列構造二叉樹][分治][遞歸]

3. 多畫圖 寫寫寫 遍歷代碼 手撕變量 大腦保持清醒

轉載鏈接:https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/solution/hou-xu-bian-li-python-dai-ma-java-dai-ma-by-liwe-2/
轉載鏈接:https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/solution/tu-jie-gou-zao-er-cha-shu-wei-wan-dai-xu-by-user72/

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的[Leetcode][第106题][JAVA][ 从中序与后序遍历序列构造二叉树][分治][递归]的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。