面试题 04.02. 最小高度树
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution{
// 暴露给用户的接口
public TreeNode sortedArrayToBST(int[] nums) {
return sortedArrayToBST(nums, 0, nums.length);
}
// 内部的实现
private TreeNode sortedArrayToBST(int[] nums, int start, int end) {
// 递归终止条件
if (start == end) {
return null;
}
int mid = (end - start) / 2 + start; // 防止溢出
TreeNode root = new TreeNode(nums[mid]); // 注意是 nums[mid], 不是 mid
root.left = sortedArrayToBST(nums, start, mid);
root.right = sortedArrayToBST(nums, mid + 1, end);
return root;
}
}
226. 翻转二叉树
class Solution {
public TreeNode invertTree(TreeNode root) {
if(root == null) {
return null;
}
TreeNode l = invertTree(root.left);
TreeNode r = invertTree(root.right);
root.left = r;
root.right = l;
return root;
}
}
938. 二叉搜索树的范围和
看到二叉排序树就中序遍历。
这题目我刚开始看错了,以为是找到位置在 L 和 R 之间的数的和,感觉好难的样子,其实只是比较值而已。
class Solution {
public int rangeSumBST(TreeNode root, int L, int R) {
if(root == null) {
return 0;
}
if (L <= root.val && root.val <= R) {
return root.val + rangeSumBST(root.left, L, R) + rangeSumBST(root.right, L, R);
} else if(root.val < L) {
return rangeSumBST(root.right, L, R);
} else {
return rangeSumBST(root.left, L, R);
}
}
}
617. 合并二叉树
class Solution {
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
if(t2 == null){
return t1;
}
if(t1 == null){
return t2;
}
t1.val += t2.val; // 此处需要注意,可以不需要新加一个 TreeNode,直接用 t1 相加
t1.left = mergeTrees(t1.left, t2.left);
t1.right = mergeTrees(t1.right, t2.right);
return t1;
}
}
700. 二叉搜索树中的搜索
在这里我忽视了二叉搜索树的大小,左边的数比 root 小。
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
while (root != null && val != root.val) {
root = val < root.val ? root.left : root.right;
}
return root;
}
}
590. N叉树的后序遍历
个人认为如果单纯是解题的话,没必要非得用迭代法。实际工作的话,递归可能会有效率低的原因。
// 迭代法
class Solution {
public List<Integer> postorder(Node root) {
Stack<Node> stack = new Stack<>();
LinkedList<Integer> output = new LinkedList<>();
if (root == null) {
return output;
}
stack.add(root);
while (!stack.isEmpty()) {
Node node = stack.pop();
// 此处迭代,所以值要放到最下面
output.addFirst(node.val);
for (Node item : node.children) {
if (item != null) {
stack.add(item);
}
}
}
return output;
}
}
剑指 Offer 54. 二叉搜索树的第k大节点
如果不使用 list 记录值的大小的话,那就势必要用到全局变量。我一向是很不喜欢随意用到全局变量的。
class Solution {
public int kthLargest(TreeNode root, int k) {
//二叉搜索树的中序遍历, 利用 list 记录值的大小,缺点是需要找到所有数值
List<Integer> resultList = new ArrayList();
traveTree(root, resultList);
return resultList.get(resultList.size() - k);
}
void traveTree(TreeNode root,List<Integer> resultList){
if(null != root.left){
traveTree(root.left, resultList);
}
// 在这里是可以获知数值大小的。
resultList.add(root.val);
if(null != root.right){
traveTree(root.right, resultList);
}
}
}
1379. 找出克隆二叉树中的相同节点
这个作者大大的代码很厉害,简洁优美,直达此题本质。而我这种菜鸡就只能根据值来计算还算不对嘤嘤嘤!他还写了迭代法和层次遍历法。
class Solution {
public final TreeNode getTargetCopy(final TreeNode original, final TreeNode cloned, final TreeNode target) {
if (original == null){
return null;
}
if (original == target){
return cloned;
}
// 递归左子树
TreeNode res = getTargetCopy(original.left, cloned.left, target);
if (res != null){
return res;
}
// 递归右子树
res = getTargetCopy(original.right, cloned.right, target);
if (res != null){
return res;
}
return null;
}
}
// 作者:burning-summer
// 链接:https://leetcode-cn.com/problems/find-a-corresponding-node-of-a-binary-tree-in-a-clone-of-that-tree/solution/tong-shi-bian-li-liang-ke-shu-jin-xing-jie-dian-pi/
// 来源:力扣(LeetCode)
// 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。