有一棵有
个节点的二叉树,其根节点为
。修剪规则如下:
1.修剪掉当前二叉树的叶子节点,但是不能直接删除叶子节点
2.只能修剪叶子节点的父节点,修剪了父节点之后,叶子节点也会对应删掉
3.如果想在留下尽可能多的节点前提下,修剪掉所有的叶子节点。请你返回修剪后的二叉树。
有如下二叉树:
o
/ \
o o
/ \ / \
o o o o 修剪过后仅会留下根节点。
o
/ \
o o
/ \ / \
o o o o {1,1,1,1,1,1,1}{1}
叶子节点为最下面的4个1节点,但是不能直接修剪,只能修剪中间的2个1,修剪掉之后,只有根节点了
{1,#,1,#,1,#,1,#,1}{1,#,1,#,1}
退化为一条链了,将最后两个节点删除。
,删除根节点时返回为空。
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return TreeNode类
*/
TreeNode* pruneLeaves(TreeNode* root) {
// write code here
if (root == nullptr) {
return nullptr;
}
if (root->left == nullptr && root->right == nullptr) {
return nullptr;
}
if (root->left != nullptr && root->left->left == nullptr && root->left->right == nullptr) {
return nullptr;
}
if (root->right != nullptr && root->right->left == nullptr && root->right->right == nullptr) {
return nullptr;
}
root->left = pruneLeaves(root->left);
root->right = pruneLeaves(root->right);
return root;
}
}; import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return TreeNode类
*/
public TreeNode pruneLeaves (TreeNode root) {
// 以下三种情况会将当前节点修剪掉
// 当前节点是空节点
if(root == null)
return null;
// 左孩子是叶子节点
if(root.left != null && root.left.left == null && root.left.right == null)
return null;
// 右节点是叶子节点
if(root.right != null && root.right.left == null && root.right.right == null)
return null;
root.left = pruneLeaves(root.left);
root.right = pruneLeaves(root.right);
return root;
}
} class Solution {
private:
bool isLeaf(TreeNode *root) {
return root && !root->left && !root->right;
}
public:
TreeNode* pruneLeaves(TreeNode* root) {
if (!root || isLeaf(root) || isLeaf(root->left) || isLeaf(root->right))
return nullptr;
root->left = pruneLeaves(root->left);
root->right = pruneLeaves(root->right);
return root;
}
};
public static TreeNode pruneLeaves(TreeNode root) {
// write code here
if (minDepth(root) < 3) return null;
root.left = pruneLeaves(root.left);
root.right = pruneLeaves(root.right);
return root;
}
// 根节点不能为叶子结点
public static int minDepth(TreeNode root) {
// 层序遍历
Deque<TreeNode> queue = new ArrayDeque<>();
if (root != null) {
if (root.left != null)
queue.add(root.left);
if (root.right != null)
queue.add(root.right);
}
if (queue.isEmpty()) return 1;
int minDepth = 1;
while (!queue.isEmpty()) {
boolean isLeaf = false;
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode curNode = queue.poll();
assert curNode != null;
if (curNode.left != null) queue.offer(curNode.left);
if (curNode.right != null) queue.offer(curNode.right);
if (curNode.left == null && curNode.right == null) {
isLeaf = true;
break;
}
}
minDepth++;
if (isLeaf) return minDepth;
}
return minDepth;
}v import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* public TreeNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return TreeNode类
*/
public static TreeNode pruneLeaves(TreeNode root) {
// write code here
if (minDepth(root) < 3) return null;
if (root.left != null) root.left = pruneLeaves(root.left);
if (root.right != null) root.right = pruneLeaves(root.right);
return root;
}
public static int minDepth(TreeNode root) {
if (root == null)
return Integer.MAX_VALUE;
if (root.left == null && root.right == null)
return 1;
return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
}
} # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param root TreeNode类 # @return TreeNode类 # class Solution: def pruneLeaves(self , root: TreeNode) -> TreeNode: # write code here mark = set() self.traverse(root, mark) if root in mark: return None self.cut(root, mark) return root def traverse(self, root, mark): if root is None: return False if root.left is None and root.right is None: mark.add(root) return True if self.traverse(root.left, mark): mark.add(root) return False if self.traverse(root.right, mark): mark.add(root) return False return False def cut(self, root, mark): if root is None: return if root in mark: return if root.left in mark: root.left = None self.cut(root.left, mark) if root.right in mark: root.right = None self.cut(root.right, mark)
class Solution:
def pruneLeaves(self , root ):
def isLeaf(root):
# 判断节点是否为叶子节点
if root is not None and root.left is None and root.right is None:
return True
return False
# 基础情况
if root is None:
return None
# 如果遇到叶子节点,则修剪父节点
if isLeaf(root.left) or isLeaf(root.right):
return None
root.left = self.pruneLeaves(root.left)
root.right = self.pruneLeaves(root.right)
return root
package main
import _"fmt"
import . "nc_tools"
/*
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return TreeNode类
*/
func pruneLeaves( root *TreeNode ) *TreeNode {
if check(root){
return nil
}
if root.Left!=nil{
if check(root.Left){
root.Left=nil
}else{
pruneLeaves(root.Left)
}
}
if root.Right!=nil{
if check(root.Right){
root.Right=nil
}else{
pruneLeaves(root.Right)
}
}
return root
}
//检查是否叶子节点的父节点
func check(root *TreeNode)bool{
if root.Left!=nil{
if root.Left.Left==nil&&root.Left.Right==nil{
return true
}
}
if root.Right!=nil{
if root.Right.Left==nil&&root.Right.Right==nil{
return true
}
}
return false
}
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param root TreeNode类 # @return TreeNode类 # class Solution: def pruneLeaves(self , root ): # write code here def find(tree): if not tree.right and not tree.left: tree.val = 2 if tree.right: find(tree.right) if tree.left: find(tree.left) find(root) def find2(tree): if tree.right: if tree.right.val == 2: tree.val = 3 else: find2(tree.right) if tree.left: if tree.left.val == 2: tree.val = 3 else: find2(tree.left) find2(root) def find3(tree): if tree.right: if tree.right.val != 1: tree.right = None else: find3(tree.right) if tree.left: if tree.left.val != 1: tree.left = None else: find3(tree.left) find3(root) if root.val == 2&nbs***bsp;root.val == 3: return None return root
struct TreeNode*delete(struct TreeNode* root){
if(!root) return root;
if(root->left){
if(root->left->left==NULL&&root->left->right==NULL) return NULL;
}
if(root->right){
if(root->right->left==NULL&&root->right->right==NULL) return NULL;
}
root->left=delete(root->left);
root->right=delete(root->right);
return root;
}
struct TreeNode* pruneLeaves(struct TreeNode* root) {
if(!root) return root;
if(root->left==NULL&&root->right==NULL) return root;
return delete(root);
} import java.util.*;
public class Solution {
public TreeNode pruneLeaves (TreeNode root) {
if (isLeaf(root.left) || isLeaf(root.right)) {
root = null;
return null;
}
if (root.left != null) { root.left = pruneLeaves(root.left); }
if (root.right != null) { root.right = pruneLeaves(root.right); }
return root;
}
private boolean isLeaf (TreeNode root) {
if (root == null) { return false; }
return root.left == null && root.right == null;
}
} TreeNode pruneLeaves(TreeNode root) { if (dfs(root)) return null; else return root; } boolean dfs(TreeNode root) { if (root==null) return false; if (isLeaf(root.left)||isLeaf(root.right)) return true; boolean left=dfs(root.left); if (left) root.left=null; boolean right=dfs(root.right); if (right) root.right=null; return false; } boolean isLeaf(TreeNode root) { return root!=null&&root.left==null&&root.right==null; }
class Solution(): def findpath(self, node): if node is None: return None if node.left is None and node.right is not None: if node.right.left is None and node.right.right is None: return if node.right is None and node.left is not None: if node.left.left is None and node.left.right is None: return if node.left is not None and node.right is not None: if (node.left.left is None and node.left.right is None)&nbs***bsp;(node.right.left is None and node.right.right is None): return root = TreeNode(node.val) root.left = self.findpath(node.left) root.right = self.findpath(node.right) return root