C/C++教程

Leetcode每日随机2021/4/18

本文主要是介绍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('#');
		}
	}
}
这篇关于Leetcode每日随机2021/4/18的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!