LeetCode 剑指 Offer II 047. 二叉树剪枝
文章目录
题目描述
LeetCode 剑指 Offer II 047. 二叉树剪枝
提示:
一、解题关键词
二、解题报告
1.思路分析
2.时间复杂度
3.代码示例
/**
* 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 {
public TreeNode pruneTree(TreeNode root) {
//本质为树的遍历
if(null == root) return root;
root.left = pruneTree(root.left);
root.right = pruneTree(root.right);
if(root.val == 0 && root.left == null && root.right == null){
root = null;
}
return root;
}
}
2.知识点
树的遍历
1、递归遍历
2、非递归遍历
总结
相同题目
814. 二叉树剪枝