后序遍历
/**
* 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 (root == null){
return null;
}
root.left = pruneTree(root.left);
root.right = pruneTree(root.right);
if ( root.left == null && root.right == null && root.val == 0){
return null;
}
return root;
}
}

该博客主要介绍了二叉树的后序遍历算法,并提供了一个Java实现的解决方案,用于修剪值为0且没有子节点的树节点。在深入探讨算法细节的同时,也展示了如何递归地处理树结构。
217

被折叠的 条评论
为什么被折叠?



