Leetcode每日随机2021/4/18

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

今天这两题都是树,写的不是很满意,又臭又长的
leetcode1325
我是这样想的:

  1. 把val为target的节点和他们的父节点都找出来放到一个列表里
  2. 把target叶子节点从列表中刹车农户,同时其val设为0
  3. 把val为0的孩子置为null
  4. 重复23直至列表大小不在变化

leetcode95
我是这样想的:
5. 先把n的全排列全算一遍
6. 按照每一种排列的顺序构造一颗二叉搜索树
7. 因为可能重复(同一种排列构造出来的二叉搜索树是一样的,如213和231),用扩充二叉树唯一标识一棵树,再用set去下重即可

代码

leetcode1325

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    List<TreeNode> list = new ArrayList<TreeNode>();

	public TreeNode removeLeafNodes(TreeNode root, int target) {
		dfs(root, target);
		int size = list.size();
		while (true) {
			for (int i = 0; i < list.size(); i++) {
				TreeNode node = list.get(i);
				if (node.left != null && node.left.val == 0) {
					node.left = null;
				}
				if (node.right != null && node.right.val == 0) {
					node.right = null;
				}
				if (node.left == null && node.right == null && node.val == target) {
					list.set(i, null);
					node.val = 0;
				}
			}
			while (list.remove(null))
				;
			if (list.size() == size) {
				break;
			}
			size = list.size();
		}
        if (root.val == 0) {
			return null;
		}
		return root;
	}

	private void dfs(TreeNode node, int target) {
		if (node != null) {
			if (node.val == target) {
				list.add(node);
			}
			if (node.left != null) {
				if (!list.contains(node) && node.left.val == target) {
					list.add(node);
				}
				dfs(node.left, target);
			}
			if (node.right != null) {
				if (!list.contains(node) && node.right.val == target) {
					list.add(node);
				}
				dfs(node.right, target);
			}
		}
	}
}

leetcode95

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    List<TreeNode> list = new ArrayList<TreeNode>();

	public List<TreeNode> generateTrees(int n) {
		List<String> permutations = new ArrayList<String>();
		Set<String> trees = new HashSet<String>();
		permutation(n, permutations, "");
		for (String tree : permutations) {
			TreeNode root = new TreeNode(tree.charAt(0) - '0');
			for (int i = 1; i < tree.length(); i++) {
				int val = tree.charAt(i) - '0';
				build(root, val);
			}
			StringBuilder sb = new StringBuilder();
			tree2String(root, sb);
			String extendTree = sb.toString();
			if (!trees.contains(extendTree)) {
				trees.add(extendTree);
				list.add(root);
			}
		}
		return list;
	}

	// 向二插搜索树中插入节点
	private void build(TreeNode root, int val) {
		if (val > root.val) {
			if (root.right == null) {
				root.right = new TreeNode(val);
			} else {
				build(root.right, val);
			}
		} else {
			if (root.left == null) {
				root.left = new TreeNode(val);
			} else {
				build(root.left, val);
			}
		}
	}

	// 全排列
	private void permutation(int n, List<String> list, String temp) {
		if (temp.length() == n) {
			list.add(temp);
		} else {
			for (int i = 1; i <= n; i++) {
				if (temp.contains(String.valueOf(i))) {
					continue;
				}
				permutation(n, list, temp + i);
			}
		}
	}

	// 将二叉树用扩充二叉树的字符串形式表示
	private void tree2String(TreeNode root, StringBuilder sb) {
		sb.append(root.val);
		if (root.left != null) {
			tree2String(root.left, sb);
		} else {
			sb.append('#');
		}
		if (root.right != null) {
			tree2String(root.right, sb);
		} else {
			sb.append('#');
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值