LeetCode 106~110
生活随笔
收集整理的這篇文章主要介紹了
LeetCode 106~110
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
前言
本文隸屬于專欄《LeetCode 刷題匯總》,該專欄為筆者原創(chuàng),引用請注明來源,不足和錯誤之處請?jiān)谠u論區(qū)幫忙指出,謝謝!
本專欄目錄結(jié)構(gòu)請見LeetCode 刷題匯總
Github 配套工程
algorithm
正文
幕布
幕布鏈接
106. 從中序與后序遍歷序列構(gòu)造二叉樹
題解
官方題解?
遞歸
package com.shockang.study.algorithm.java.leetcode.leetcode101_200.leetcode106.solution1;import com.shockang.study.algorithm.java.leetcode.common.TreeNode;import java.util.HashMap; import java.util.Map;/*** 遞歸** @author Shockang*/ public class Solution {int postIdx;int[] postorder;int[] inorder;Map<Integer, Integer> idxMap = new HashMap<>();public TreeNode buildTree(int[] inorder, int[] postorder) {this.postorder = postorder;this.inorder = inorder;// 從后序遍歷的最后一個元素開始postIdx = postorder.length - 1;// 建立(元素,下標(biāo))鍵值對的哈希表int idx = 0;for (Integer val : inorder) {idxMap.put(val, idx++);}return helper(0, inorder.length - 1);}public TreeNode helper(int inLeft, int inRight) {// 如果這里沒有節(jié)點(diǎn)構(gòu)造二叉樹了,就結(jié)束if (inLeft > inRight) {return null;}// 選擇 post_idx 位置的元素作為當(dāng)前子樹根節(jié)點(diǎn)int rootVal = postorder[postIdx];TreeNode root = new TreeNode(rootVal);// 根據(jù) root 所在位置分成左右兩棵子樹int index = idxMap.get(rootVal);// 下標(biāo)減一postIdx--;// 構(gòu)造右子樹root.right = helper(index + 1, inRight);// 構(gòu)造左子樹root.left = helper(inLeft, index - 1);return root;} }迭代
package com.shockang.study.algorithm.java.leetcode.leetcode101_200.leetcode106.solution2;import com.shockang.study.algorithm.java.leetcode.common.TreeNode;import java.util.Deque; import java.util.LinkedList;/*** 迭代** @author Shockang*/ public class Solution {public TreeNode buildTree(int[] inorder, int[] postorder) {if (postorder == null || postorder.length == 0) {return null;}TreeNode root = new TreeNode(postorder[postorder.length - 1]);Deque<TreeNode> stack = new LinkedList<>();stack.push(root);int inorderIndex = inorder.length - 1;for (int i = postorder.length - 2; i >= 0; i--) {int postorderVal = postorder[i];TreeNode node = stack.peek();if (node.val != inorder[inorderIndex]) {node.right = new TreeNode(postorderVal);stack.push(node.right);} else {while (!stack.isEmpty() && stack.peek().val == inorder[inorderIndex]) {node = stack.pop();inorderIndex--;}node.left = new TreeNode(postorderVal);stack.push(node.left);}}return root;} }107. 二叉樹的層序遍歷 II
題解
「代碼隨想錄」二叉樹層序遍歷登場:我要打十個!?
遞歸 + list.add(0, x)
package com.shockang.study.algorithm.java.leetcode.leetcode101_200.leetcode107.solution1;import com.shockang.study.algorithm.java.leetcode.common.TreeNode;import java.util.ArrayList; import java.util.List;/*** 遞歸 + list.add(0, x)** @author Shockang*/ public class Solution {public List<List<Integer>> levelOrderBottom(TreeNode root) {List<List<Integer>> list = new ArrayList<>();helper(list, root, 0);return list;}private void helper(List<List<Integer>> list, TreeNode node, int level) {if (node == null) {return;}if (list.size() > level) {list.get(list.size() - level - 1).add(node.val);} else {List<Integer> cur = new ArrayList<>();cur.add(node.val);list.add(0, cur);}helper(list, node.left, level + 1);helper(list, node.right, level + 1);} }迭代 + reverse
package com.shockang.study.algorithm.java.leetcode.leetcode101_200.leetcode107.solution2;import com.shockang.study.algorithm.java.leetcode.common.TreeNode;import java.util.*;/*** 迭代 + reverse** @author Shockang*/ public class Solution {public List<List<Integer>> levelOrderBottom(TreeNode root) {List<List<Integer>> res = new ArrayList<>();if (root == null) return res;Queue<TreeNode> queue = new LinkedList<>();queue.add(root);while (!queue.isEmpty()) {List<Integer> level = new ArrayList<>();int levelCount = queue.size();for (int i = 0; i < levelCount; i++) {TreeNode node = queue.poll();if (node.left != null) {queue.add(node.left);}if (node.right != null) {queue.add(node.right);}level.add(node.val);}res.add(level);}Collections.reverse(res);return res;} }迭代 + list.add(0, x)
package com.shockang.study.algorithm.java.leetcode.leetcode101_200.leetcode107.solution3;import com.shockang.study.algorithm.java.leetcode.common.TreeNode;import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Queue;/*** 迭代 + list.add(0, x)** @author Shockang*/ public class Solution {public List<List<Integer>> levelOrderBottom(TreeNode root) {List<List<Integer>> levelOrder = new LinkedList<>();if (root == null) return levelOrder;Queue<TreeNode> queue = new LinkedList<>();queue.offer(root);while (!queue.isEmpty()) {List<Integer> level = new ArrayList<>();int size = queue.size();for (int i = 0; i < size; i++) {TreeNode node = queue.poll();level.add(node.val);TreeNode left = node.left, right = node.right;if (left != null) {queue.offer(left);}if (right != null) {queue.offer(right);}}levelOrder.add(0, level);}return levelOrder;} }108. 將有序數(shù)組轉(zhuǎn)換為二叉搜索樹
題解
官方題解?
中序遍歷,中間左邊數(shù)字根節(jié)點(diǎn)
package com.shockang.study.algorithm.java.leetcode.leetcode101_200.leetcode108.solution1;import com.shockang.study.algorithm.java.leetcode.common.TreeNode;/*** 中序遍歷** @author Shockang*/ public class Solution {public TreeNode sortedArrayToBST(int[] nums) {return helper(nums, 0, nums.length - 1);}public TreeNode helper(int[] nums, int left, int right) {if (left > right) {return null;}// 總是選擇中間位置左邊的數(shù)字作為根節(jié)點(diǎn)int mid = (left + right) / 2;TreeNode root = new TreeNode(nums[mid]);root.left = helper(nums, left, mid - 1);root.right = helper(nums, mid + 1, right);return root;} }109. 有序鏈表轉(zhuǎn)換二叉搜索樹
題解
官方題解?
遞歸,快慢指針找中點(diǎn)
package com.shockang.study.algorithm.java.leetcode.leetcode101_200.leetcode109.solution1;import com.shockang.study.algorithm.java.leetcode.common.ListNode; import com.shockang.study.algorithm.java.leetcode.common.TreeNode;/*** 遞歸,快慢指針找中點(diǎn)** @author Shockang*/ public class Solution {public TreeNode sortedListToBST(ListNode head) {if (head == null) {return null;}return helper(head, null);}private TreeNode helper(ListNode head, ListNode tail) {if (head == tail) {return null;}ListNode fast = head;ListNode slow = head;while (fast != tail && fast.next != tail) {fast = fast.next.next;slow = slow.next;}TreeNode root = new TreeNode(slow.val);root.left = helper(head, slow);root.right = helper(slow.next, tail);return root;} }110. 平衡二叉樹
題解
官方題解?
↓ 遞歸
package com.shockang.study.algorithm.java.leetcode.leetcode101_200.leetcode110.solution1;import com.shockang.study.algorithm.java.leetcode.common.TreeNode;/*** ↓ 遞歸** @author Shockang*/ public class Solution {public boolean isBalanced(TreeNode root) {if (root == null) {return true;} else {return Math.abs(height(root.left) - height(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right);}}public int height(TreeNode root) {if (root == null) {return 0;} else {return Math.max(height(root.left), height(root.right)) + 1;}} }↑ 遞歸
package com.shockang.study.algorithm.java.leetcode.leetcode101_200.leetcode110.solution2;import com.shockang.study.algorithm.java.leetcode.common.TreeNode;/*** ↑ 遞歸** @author Shockang*/ public class Solution {public boolean isBalanced(TreeNode root) {return height(root) >= 0;}public int height(TreeNode root) {if (root == null) {return 0;}int leftHeight = height(root.left);int rightHeight = height(root.right);if (leftHeight == -1 || rightHeight == -1 || Math.abs(leftHeight - rightHeight) > 1) {return -1;} else {return Math.max(leftHeight, rightHeight) + 1;}} }總結(jié)
以上是生活随笔為你收集整理的LeetCode 106~110的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构课程设计(C语言实现)
- 下一篇: 电脑死机的原因