814. Binary Tree Pruning

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LEE18254290736/article/details/80319062
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode PostOrder(TreeNode T){
        if(T!=null){
            T.left  = PostOrder(T.left);
            T.right = PostOrder(T.right);
            if(T.left==null&&T.right==null)
                if(T.val==0)
                    T=null;
        }
        return T;
    }
    public TreeNode pruneTree(TreeNode root) {
        PostOrder(root);
        return root;
    }
    
}
阅读更多

没有更多推荐了,返回首页