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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

剑指offer之21-25题解

發布時間:2024/2/28 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 剑指offer之21-25题解 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

劍指offer之21-25題解


目錄

  • 棧的壓入,彈出序列
  • 從上往下打印二叉樹
  • 二叉搜索樹的后序遍歷序列
  • 二叉樹中和為某一值的路徑
  • 復雜鏈表的復制

  • 21. 棧的壓入,彈出序列

    (一)題目描述

    輸入兩個整數序列,第一個序列表示棧的壓入順序,請判斷第二個序列是否可能為該棧的彈出順序。假設壓入棧的所有數字均不相等。例如序列1,2,3,4,5是某棧的壓入順序,序列4,5,3,2,1是該壓棧序列對應的一個彈出序列,但4,3,5,1,2就不可能是該壓棧序列的彈出序列。(注意:這兩個序列的長度是相等的)

    (二) 思路

  • 使用一個棧來模擬壓入彈出操作。
  • stack每入棧一個元素,就去判斷棧頂元素是否與彈出數組的popIndex元素是否相同,相同就pop。
  • 最后判斷stack是否為null,為null說明彈出序列是正確的,否則錯誤。
  • (三)代碼實現

    import java.util.Stack; public class Solution {public boolean IsPopOrder(int[] pushA, int[] popA) {int n = pushA.length;Stack<Integer> stack = new Stack<>();for (int pushIndex = 0, popIndex = 0; pushIndex < n; pushIndex++) {stack.push(pushA[pushIndex]);while (popIndex < n && !stack.isEmpty() && stack.peek() == popA[popIndex]) {stack.pop();popIndex++;}}return stack.isEmpty();} }

    22. 從上往下打印二叉樹

    (一)題目描述

    從上往下打印出二叉樹的每個節點,同層節點從左至右打印。

    (二) 思路

  • 創建一個隊列和list數組。
  • 當隊列不為null時獲取隊列的size,當cnt–>0,隊列彈出一個元素,將元素值加入list數組,然后添加它的左右孩子。
  • 跟著演變一遍就能看懂。
  • (三)代碼實現

    import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; public class Solution {public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {Queue<TreeNode> queue = new LinkedList<>();ArrayList<Integer> ret = new ArrayList<>();queue.offer(root);while (!queue.isEmpty()){int cnt = queue.size();while (cnt-->0){TreeNode t = queue.poll();if (t == null)continue;ret.add(t.val);queue.offer(t.left);queue.offer(t.right);}}return ret;} }

    23. 二叉搜索樹的后序遍歷序列

    (一)題目描述

    輸入一個整數數組,判斷該數組是不是某二叉搜索樹的后序遍歷的結果。如果是則輸出Yes,否則輸出No。假設輸入的數組的任意兩個數字都互不相同。

    注:二叉搜索樹中序遍歷就是遞增的,如下的中序遍歷為1,2,3.

    (二) 思路

  • 輸入的后序遍歷,最后一個即為根節點,根節點的左邊都比它小,根節點的右邊都比它大。依次遞歸。
  • (三)代碼實現

    public class Solution {public boolean VerifySquenceOfBST(int[] sequence) {if (sequence == null || sequence.length == 0) return false;return verify(sequence, 0, sequence.length - 1);}private boolean verify(int[] sequence, int first, int last) {if (last - first <= 1)return true;int rootVal = sequence[last];int cutIndex = first;while (cutIndex < last && sequence[cutIndex] <= rootVal) cutIndex++;for (int i = cutIndex; i < last; i++) if (sequence[i] < rootVal) return false;return verify(sequence, first, cutIndex - 1) && verify(sequence, cutIndex, last - 1);} }

    24. 二叉樹中和為某一值的路徑

    (一)題目描述

    輸入一顆二叉樹的跟節點和一個整數,打印出二叉樹中結點值的和為輸入整數的所有路徑。路徑定義為從樹的根結點開始往下一直到葉結點所經過的結點形成一條路徑。(注意: 在返回值的list中,數組長度大的數組靠前)

    (二) 思路

  • backtracking()方法中,當target == 0 && node.left == null && node.right == null說明已找到和為target路線,并且是最后一個節點。所以直接加入ret即可。否則遞歸回溯
  • 因為不能確定只有一條路線,所以最后path.remove(path.size() - 1)
  • (三)代碼實現

    public class Solution {private ArrayList<ArrayList<Integer>> ret = new ArrayList<>();public ArrayList<ArrayList<Integer>> FindPath(TreeNode root, int target) {backtracking(root, target, new ArrayList<Integer>());return ret;}private void backtracking(TreeNode node, int target, ArrayList<Integer> path) {if (node == null)return;path.add(node.val);target -= node.val;if (target == 0 && node.left == null && node.right == null) {ret.add(new ArrayList<>(path));} else {backtracking(node.left, target, path);backtracking(node.right, target, path);}path.remove(path.size() - 1);} }

    25. 復雜鏈表的復制

    (一)題目描述

    輸入一個復雜鏈表(每個節點中有節點值,以及兩個指針,一個指向下一個節點,另一個特殊指針指向任意一個節點),返回結果為復制后復雜鏈表的head。(注意,輸出結果中請不要返回參數中的節點引用,否則判題程序會直接返回空)

    (二) 思路

  • 在每個節點的后面插入復制的節點
  • 對復制節點的random鏈接進行賦值
  • 拆分

  • (三)代碼實現

    public class Solution {public RandomListNode Clone(RandomListNode pHead) {if (pHead == null) {return null;}//插入新節點RandomListNode cur = pHead;while (cur != null) {RandomListNode clone = new RandomListNode(cur.label);clone.next = cur.next;cur.next = clone;cur = clone.next;}//建立random連接cur = pHead;while (cur != null) {RandomListNode clone = cur.next;if (cur.random != null) clone.random = cur.random.next;cur = clone.next;}//拆分cur = pHead;RandomListNode pCloneHead = pHead.next;while (cur.next != null) {RandomListNode next = cur.next;cur.next = next.next;cur = next;}return pCloneHead;} }

    總結

    以上是生活随笔為你收集整理的剑指offer之21-25题解的全部內容,希望文章能夠幫你解決所遇到的問題。

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