106. 从中序与后序遍历序列构造二叉树

理论知识简单,如何实现
- 第一步:如果数组大小为零的话,说明是空节点了。
- 第二步:如果不为空,那么取后序数组最后一个元素作为节点元素。
- 第三步:找到后序数组最后一个元素在中序数组的位置,作为切割点
- 第四步:切割中序数组,切成中序左数组和中序右数组 (顺序别搞反了,一定是先切中序数组)
- 第五步:切割后序数组,切成后序左数组和后序右数组
第六步:递归处理左区间和右区间
class Solution {public TreeNode buildTree(int[] inorder, int[] postorder) {return buildFromInPost(inorder, 0, inorder.length, postorder, 0, postorder.length);}public TreeNode buildFromInPost(int[] inorder, int inLeft, int inRight,int[] postorder, int postLeft, int postRight){if (postRight == postLeft) {return null;}//后序数组postorder里最后一个即为根结点int rootVal = postorder[postRight - 1];TreeNode root = new TreeNode(rootVal);if (postRight - postLeft == 1) {return root;}//在中序数组inorder中找到根结点值所在的位置int rootIndex = 0;for (rootIndex = inLeft; rootIndex < inRight; ++rootIndex) {if (inorder[rootIndex] == rootVal) {break;}}//根据rootIndex划分左右子树继续递归root.left = buildFromInPost(inorder, inLeft, rootIndex,postorder, postLeft, postLeft + (rootIndex - inLeft));root.right = buildFromInPost(inorder, rootIndex + 1, inRight,postorder, postLeft + (rootIndex - inLeft), postRight - 1);return root;}}
105. 从前序与中序遍历序列构造二叉树
把四个区间搞清楚这个题就写完一半了
class Solution {public TreeNode buildTree(int[] preorder, int[] inorder) {return buildFromPreIn(preorder, 0, preorder.length,inorder, 0, inorder.length);}public TreeNode buildFromPreIn(int[] preorder, int preLeft, int preRight,int[] inorder, int inLeft, int inRight){if (preRight == preLeft) {return null;}//前序数组preorder里第一个即为根结点int rootVal = preorder[preLeft];TreeNode root = new TreeNode(rootVal);if (preRight - preLeft == 1) {return root;}//在中序数组inorder中找到根结点值所在的位置int rootIndex = 0;for (rootIndex = inLeft; rootIndex < inRight; ++rootIndex) {if (inorder[rootIndex] == rootVal) {break;}}//根据rootIndex划分左右子树继续递归//左闭右开区间root.left = buildFromPreIn(preorder, preLeft + 1, preLeft + 1 +(rootIndex - inLeft),inorder, inLeft, rootIndex);root.right = buildFromPreIn(preorder, preLeft + 1 + (rootIndex - inLeft), preRight,inorder, rootIndex + 1, inRight);return root;}}
654. 最大二叉树
class Solution {public TreeNode constructMaximumBinaryTree(int[] nums) {return buildTree(nums, 0, nums.length);}public TreeNode buildTree(int[] nums, int left, int right) {if (right == left) {return null;}int rootVal = Integer.MIN_VALUE;int rootIndex = 0;for (int i = left; i < right; ++i) {if (nums[i] > rootVal) {rootVal = nums[i];rootIndex = i;}}TreeNode root = new TreeNode(rootVal);root.left = buildTree(nums, left, rootIndex);root.right = buildTree(nums, rootIndex + 1, right);return root;}}
617. 合并二叉树
递归法,修改原树的结构
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {if (root1 == null) return root2;if (root2 == null) return root1;root1.val += root2.val;root1.left = mergeTrees(root1.left, root2.left);root1.right = mergeTrees(root1.right, root2.right);return root1;}
递归法,不修改原树的结构
public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {if (root1 == null) return root2;if (root2 == null) return root1;TreeNode root = new TreeNode(root1.val + root2.val);root.left = mergeTrees(root1.left, root2.left);root.right = mergeTrees(root1.right, root2.right);return root;}
